Following are the functions in node.js using promise.
exports.getDetailsByUserId = function(req,res) {
getUserDetails(req, res) // function to get user details
.then(function (Details) {
var allpromises = Details.map(getStatus); //get and update the status of user
return allpromises
})
.then(function (promises) {
var orderresult =[];
Q.allSettled(promises)
.then(function (allpromises) {
orderresult.push(allpromises);
})
return orderresult;
})
.then(function (result) {
getUserDetails(req, res)//again calling get user details so that i will get modified result
.then(function (data) {
res.json({
"User" : data
})
})
})
.catch(function (error) {
console.log(error);
res.status(200).json({
"Message": error
})
})
}
var getStatus = function (data,req) {
var deferred = Q.defer();
requestify.post('http://example.com', {
"data": [{ "Number": No }]
})
.then(function (response) {
response.getBody();
response.body;
var Status = JSON.parse(response.body);
var Details = { "_id": data._id, "Status": Status.status[0] };
return OrderDetails;
})
.then(function (DetailsData){
var req = { body : { Details :DetailsData} };
updateDetails(req).then(function (data) {
deferred.resolve;
})
})
.catch(function (error) {
deferred.resolve(error);
});
return deferred.promise;
}
The getUserDetails
function gets details of user first time, then getStatus
calls another API and updates related user status. To this point every thing is working fine.
I need to call getUserDetails
a second time to get updated results, but it does not give update results. Maybe it gets called before updating the status? What might be wrong with the code so that it will execute after Q.all
?
Thank you.