In real code, you would call makePromise().then()
or await makePromise()
. I assigned the promise to an intermediate variable to illustrate the difference. Which style do you prefer?
const cl = console.log
function thenCatch () {
cl('start')
const promise = makePromise()
promise
.then(response => {
cl('response')
})
.catch(err => {
})
.finally(() => {
})
cl('end')
}
thenCatch()
// start
// end
// response
//
// 'response' logged after 'end'. Extremely common source of confusion.
async function asyncAwait () {
cl('start')
const promise = makePromise()
try {
const response = await promise
cl('response')
} catch (err) {
} finally {
}
cl('end')
}
asyncAwait()
// start
// response
// end
//
// Logs are in order. Nice!
I have just gotten pretty comfortable with the first style, so I would say the first. However, I know as soon as I start using the second, I won't be going back haha. Thanks for the example!