I am new to using promises with bluebird. I am trying to resolve a promise when the status response is changed to "success" from the api. Below is my code:
exports.getdata(taskCreation, headers) {
var deferred = Promise.pending();
var headers = {
"Authorization": "Secret xxxxxxxxxxxxxxxxx"
};
while (true) {
request.get({
url: "https://dragon.stupeflix.com/v2/status",
qs: {
tasks: taskCreation[0]["key"]
},
headers: headers,
json: true
}, function (error, httpObj, taskStatusAndResult) {
if (!error && httpObj.statusCode == 200) {
console.log(taskStatusAndResult[0]["status"]); //contains either "queued", "executing", "success", or "error"
if (taskStatusAndResult[0]["status"] == "success")
deferred.resolve(taskStatusAndResult);
} else {
deferred.reject(error);
}
})
}
return deferred.promise;
}
api takes few seconds to process video,generate videourl and give status as "success".Until then i want to repeatedly keep calling the api,and resolve the promise only if the status is "success". My code with an infinite while loop do not work. Any suggestion on how to achieve this requirement in the best possible way.