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) {
const result = [];
for (let i = 0; i < array.length; i ++) {
return new Promise((resolve, reject) => {
const results = [];
let i = 0;
function loop() {
if (i === array.length) return resolve(results);
const item = array[i];
const itemResult = await callback(item, i, array);
result.push(itemResult);
callback(item, i, array).then(result => {
results.push(result);
i ++;
loop();
}).catch(reject);
}
return result;
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;
// }