Created
December 14, 2022 06:15
-
-
Save Zeko369/02369a9470114ad13096bc56a56ad382 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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