Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Created December 14, 2022 06:15
Show Gist options
  • Save Zeko369/02369a9470114ad13096bc56a56ad382 to your computer and use it in GitHub Desktop.
Save Zeko369/02369a9470114ad13096bc56a56ad382 to your computer and use it in GitHub Desktop.
// utils
async function* foo() {
for (let i = 0; i < 3; i++) {
await new Promise((r) => setTimeout(r, 1000));
yield i;
}
}
const subToIter = (cb) => {
const it = foo();
const next = () => {
it.next().then((item) => {
if (!item.done) {
cb(item.value);
next();
}
});
};
next();
};
(async () => {
console.log("before");
subToIter((x) => console.log("sub", x));
console.log("after");
console.log("before for await");
for await (const x of foo()) {
console.log("for", x);
}
console.log("after for await");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment