dimanche 8 mars 2015

How to use Bluebird promisification with generators + parrell promises


Vote count:

0




Trying to fire off mutiple requests off to the beats api using bluebird as well as koa for generators.


After reading some documentation I figured the following would work



var request = require('co-request'),
_ = require('lodash'),
Promise = require('bluebird');
request = Promise.promisifyAll(request);

module.exports.getTracks = function *tracks(){
firstCall = yield makeAPICall('users/' + me + '/mymusic/tracks?limit=150');
total = firstCall.body.info.total;
total -= 150;
tracks = firstCall.body.data;

//Beats only allows a maximum of 150 tracks per call
//If more tracks are needed then the remainder is called in sets of 150
var offset = 150;
while (total > 0) {
promises.push(makeAPICall('users/' + me + '/mymusic/tracks?limit=150&offset=' + offset));
offset += 150;
total -= 150;
}

var responses = yield(Promise.all(promises));
}

function makeAPICall (query){
var authOptions = {
url: 'http://ift.tt/1HkH0k7' + query,
headers: { 'Authorization': 'Bearer ' + accessToken },
json: true
};
return request.get(authOptions);
}


The method makeAPI call works as expected used with firstCall, but for some reason when I start placing the makeAPICall method into the array they never seem to execute. The variable responses yields out just an array of functions instead of an array of responses from the beats api. What do I need to change to make responses return an array of objects similar to that of firstCall?



asked 29 secs ago







How to use Bluebird promisification with generators + parrell promises

Aucun commentaire:

Enregistrer un commentaire