Skip to content

Instantly share code, notes, and snippets.

@dat-boris
Last active September 15, 2018 17:09
Show Gist options
  • Select an option

  • Save dat-boris/b6eaac9e8a92d7dec25397c286074da4 to your computer and use it in GitHub Desktop.

Select an option

Save dat-boris/b6eaac9e8a92d7dec25397c286074da4 to your computer and use it in GitHub Desktop.
A small async-await puzzle
/**
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