I'm trying to chain a some pairs of requests:
I search for game A (http request), Then I'm updating it's X to Y.
I search for game B, Then update it's property Z.
I search for game A (again), But the search result is already evaluated, before my update. So, I'm trying to update it's Y property, which is not there.
In terms of promises I want something like: A - Am --then-- B - Bm --then-- C - Cm
but my requests look like: A - B -> C --then-- Am - Bm - Cm
How this should be done correctly?
var requests =[];
matches.forEach(function(match) {
var matchPromise = new Promise(function(mResolve, mReject) {
var gamePromise = new Promise(function(resolve, reject) {
GameService.find(token, id, function(err, game) {
//this is a callback of an http request
resolve(game);
}
});
gamePromise.then(function(game) {
//irrelevant logic here
GameService.matchUpdate(token, id, mId, function(err, results) {
resolve();
})
}
});
requests.push(matchPromise);
});
Q.all(requests).then(function() {
return res.json({"status": "OK"});
});
via cristifilip
No comments:
Post a Comment