Created
May 14, 2020 14:25
-
-
Save DanShappir/3e6af0a875ceb929d05c5b1f6f87385e 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
function fromCallback(emitter) { | |
let values; | |
let resolve; | |
const init = r => [values, resolve] = [[], r]; | |
let valuesAvailable = new Promise(init); | |
emitter(value => { | |
values.push(value); | |
resolve(values); | |
}); | |
return { | |
[Symbol.asyncIterator]: async function*() { | |
for (;;) { | |
const vs = await valuesAvailable; | |
valuesAvailable = new Promise(init); | |
yield* vs; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment