If you want to execute code after a callback is called in JavaScript, then you can just place it after the callback:
function demoCallback(callback) {
callback()
console.log("I execute second")
}
demoCallback(() => {
console.log("I execute first")
})
Is it possible to do the same thing with an ES6 Promise from within the scope of the function? Let's say I have a function that returns a Promise:
function demoPromise() {
return new Promise((resolve, reject) => {
resolve()
console.log("I execute first")
})
}
demoPromise().then(() => { console.log("I execute second") })
The Code inserted after resolve executes once the Promise is resolved, but before then is called outside the scope of the function. Is there a way that I can execute code after both, but do so from within the scope of the function?