Skip to content

Instantly share code, notes, and snippets.

@snowmantw
Created November 5, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowmantw/bd9722e9a6e4320b62fb to your computer and use it in GitHub Desktop.
Save snowmantw/bd9722e9a6e4320b62fb to your computer and use it in GitHub Desktop.
await + async + generator + destruct + iterable
async function f(e) {
var p = new Promise((r) => {
setTimeout(() => {r(e)}, 1000);
})
return p;
}
function* makeSimpleGenerator(array){
var nextIndex = 0;
while(nextIndex < array.length){
yield f(array[nextIndex++]);
}
}
var gen = makeSimpleGenerator([['a', 101], ['b', 102], ['c', 103], ['d', 104]]);
async function g() {
for(var e of gen) {
var [id, num] = await e;
console.log('<<>>', id, '--->', num);
}
}
g();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment