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 await
ed.
let lastId = 1;
const id = {
then(fn, errfn) {
fn(lastId++);
}
};
async function foo() {
console.log(await id); // 1
console.log(await id); // 2
}
Not sure but for this, thats better to use generator no ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield