Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active June 13, 2018 05:30
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeck/61519fa86389179e3514af613b4b0d0d to your computer and use it in GitHub Desktop.
Save bmeck/61519fa86389179e3514af613b4b0d0d to your computer and use it in GitHub Desktop.

Avoid live "then()-ables"

Like a Promise, await will call any .then() function on its operand. This can be used to create values that change every time they are awaited.

let lastId = 1;
const id = {
  then(fn, errfn) {
    fn(lastId++);
  }
};
async function foo() {
  console.log(await id); // 1
  console.log(await id); // 2
}
@simevidas
Copy link

Just run it in Canary’s console ;)

foo

@Westixy
Copy link

Westixy commented Aug 29, 2017

Not sure but for this, thats better to use generator no ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment