Last active
September 15, 2018 17:09
-
-
Save dat-boris/b6eaac9e8a92d7dec25397c286074da4 to your computer and use it in GitHub Desktop.
A small async-await puzzle
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
| /** | |
| A small puzzle from a stupid bug today - what do you need to fill in at `XXX` to make the second assertion true? | |
| I learn quiet a bit more `async` `await` from this…. before this I intuitively sees it async and await as countering each other: | |
| • `async` wraps a promise layer | |
| • `await` unwraps a promise layer | |
| but this bug certainly expose my misunderstanding…. | |
| */ | |
| const assert = require("assert"); | |
| const idFunction = async function(x) { | |
| return x; | |
| }; | |
| const arggggg = async function() { | |
| const foo = 123; | |
| const foo2 = await idFunction(foo); | |
| assert(foo === foo2); | |
| // yay! | |
| const bar = XXX | |
| const bar2 = await idFunction(bar); | |
| assert(bar !== bar2); | |
| // boo | |
| }; | |
| arggggg(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment