Last active
October 25, 2020 03:56
-
-
Save adonishi/3c54a947401bf60fdee123e4264aa010 to your computer and use it in GitHub Desktop.
js: is 'then()' awaitable?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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