Skip to content

Instantly share code, notes, and snippets.

@Vigowebs
Last active April 15, 2022 17:43
Show Gist options
  • Save Vigowebs/06a605622e311eafcc421e3d42ef2067 to your computer and use it in GitHub Desktop.
Save Vigowebs/06a605622e311eafcc421e3d42ef2067 to your computer and use it in GitHub Desktop.
Instead of writing nested callbacks, we can write Promise (natively available from Node 4) calls.
// ❌
asyncFunc1((err, result1) => {
asyncFunc2(result1, (err, result2) => {
asyncFunc3(result2, (err, result3) => {
console.log(result3)
})
})
})
// ✔️
asyncFuncPromise1()
.then(asyncFuncPromise2)
.then(asyncFuncPromise3)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment