Created
December 20, 2017 15:28
-
-
Save jooyunghan/ae5e7ae2cdeaf8f8d139c222748a68b8 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 createAsyncIterator() { | |
const promises = []; | |
const values = []; | |
this.subscribe({ | |
next(value) { | |
if (promises.length > 0) { | |
promises.shift().resolve({ value, done: false }); | |
} else { | |
values.push(value); | |
} | |
} | |
}); | |
return { | |
next() { | |
if (values.length > 0) { | |
return { value: values.shift(), done: false }; | |
} | |
return new Promise((resolve, reject) => { | |
promises.push({ resolve, reject }); | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment