Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active Jun 13, 2018
Embed
What would you like to do?

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
}
@GuskiS
Copy link

GuskiS commented Aug 28, 2017

Can you do something like this:

const id = {
  lastId: 0,
  then(fn, errfn) {
    fn(this.lastId++);
  }
};

?

@simevidas
Copy link

simevidas commented Aug 28, 2017

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