The neatest way in my opinion is to return the promise and chain them 'down' instead of to the left, avoiding christmas tree-like callback hell.
function top() {
//promise1
return ParentPromise({
...some code here...
}).then(function(parent) {
//promise2
return ChildPromise(parent.id)
}).then(function(response) {
// do something with `result`
return response.result.items;
});
}
top().then(function(items){
// all done
});
Edit:
Or in ES6 / lambda notation;
function top() {
return ParentPromise().then(parent => {
return ChildPromise(parent.id)
}).then(response => {
return response.result.items
})
}
top().then(items => {
// all done
})
Edit: Or using Async/Await;
async function top() {
const parent = await ParentPromise()
const child = await ChildPromise(parent.id)
return child.result.items
}
top().then(items => {
// all done
})
start
related totop
? Do you wantParentPromise
to resolve toresult
?