There 3 functions first one is my promise.
fnGetIps : function () {
return new Promise(function(resolve, reject) {
var err = [], path = getBaseUrl();
restGet(path + NAMES, function (res, edges) {
if (res === undefined) {
reject("Unable to get data");
} else {
resolve(edges);
}
});
});
},
Second one is where i use my promise.
fnGetData: function(obj){
var promise_holder;
for (property in obj) {
if (obj.hasOwnProperty(property)) {
if(condition){
promise_holder = fngetip();
}
if(condition2){
//some other code
}
}
}
promise_holder.then(function (ifChildren) {
obj.children = ifChildren;
}, function (err) {
console.error(err);
});
}
And finally a function where i am calling fnGetData
TopoTree : function(children) {
var treeArray;
for (i = 0; i < children[i]; i++) {
fnGetData(children[i]);
}
treeArray.push(treeData);
return treeArray;
},
I dont want to return TreeArray
before all promises are resolved of fnGetData
.
How to wait for all promises to be resolved first and then return data?
I cannot use promise.All
as I don't have promise_holder
in topotree
scope or am I thinking in wrong direction ?
You want Promise.all()
.
Have fnGetData
return the promise_holder
and have TopoTree
collect them in an array and place the completion logic in a then function after Promise.all()
fnGetData: function(obj){
var promise_holder;
for (property in obj) {
if (obj.hasOwnProperty(property)) {
if(condition){
promise_holder = fngetip();
}
if(condition2){
//some other code
}
}
}
// RETURN THE PROMISE HERE!
return promise_holder.then(function (ifChildren) {
obj.children = ifChildren;
// Return a value for use later:
return ifChildren;
});
}
TopoTree : function(children) {
var promises = [];
for (i = 0; i < children.length; i++) {
promises.push(fnGetData(children[i]));
}
// We are handling the result / rejections here so no need to return the
// promise.
Promise.all(promises)
.then(function(treeArray) {
// Do something with treeArray
})
.catch(function(error) {
console.log(error);
});
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.