use async iterator without regenerators

This commit is contained in:
casperlamboo 2017-11-06 12:07:36 +01:00
parent 6fa71c4855
commit 4d46eb6f91

View File

@ -15,11 +15,30 @@ export function recursivePromiseApply(object, promises = [], first = true) {
} }
export async function asyncIterator(array, callback) { export async function asyncIterator(array, callback) {
const result = []; return new Promise((resolve, reject) => {
for (let i = 0; i < array.length; i ++) { const results = [];
const item = array[i]; let i = 0;
const itemResult = await callback(item, i, array);
result.push(itemResult); function loop() {
} if (i === array.length) return resolve(results);
return result;
const item = array[i];
callback(item, i, array).then(result => {
results.push(result);
i ++;
loop();
}).catch(reject);
}
loop();
});
} }
// export async function asyncIterator(array, callback) {
// const result = [];
// for (let i = 0; i < array.length; i ++) {
// const item = array[i];
// const itemResult = await callback(item, i, array);
// result.push(itemResult);
// }
// return result;
// }