Skip to content

Instantly share code, notes, and snippets.

@jooyunghan
Created December 20, 2017 15:28
Show Gist options
  • Save jooyunghan/ae5e7ae2cdeaf8f8d139c222748a68b8 to your computer and use it in GitHub Desktop.
Save jooyunghan/ae5e7ae2cdeaf8f8d139c222748a68b8 to your computer and use it in GitHub Desktop.
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