Skip to content

Instantly share code, notes, and snippets.

@adonishi
Last active October 25, 2020 03:56
Show Gist options
  • Select an option

  • Save adonishi/3c54a947401bf60fdee123e4264aa010 to your computer and use it in GitHub Desktop.

Select an option

Save adonishi/3c54a947401bf60fdee123e4264aa010 to your computer and use it in GitHub Desktop.
js: is 'then()' awaitable?
//
// is 'then()' awaitable?
// YES! this is proof of concept.
//
(async () => {
console.log('start')
function timeoutFunc() {
console.log('timeout')
}
const promise = new Promise(resolve => {
setTimeout(() => {
resolve(timeoutFunc())
}, 1000)
})
// promise.then(() => { console.log('then called') })
await promise.then(() => { console.log('then called') })
console.log('end')
})()
// result:
// start
// timeout
// then called
// end
//
// result without 'await'
// start
// end. <===
// timeout
// then called
//
//
// since then() returns Promise. then() is awaitable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment