2017-10-24 12:33:14 +02:00
|
|
|
export function recursivePromiseApply(object, promises = [], first = true) {
|
|
|
|
for (const key in object) {
|
|
|
|
const value = object[key];
|
|
|
|
|
|
|
|
if (value instanceof Promise) {
|
|
|
|
promises.push(value);
|
|
|
|
|
|
|
|
value.then(result => object[key] = result); // eslint-disable-line no-loop-func
|
|
|
|
} else if (value instanceof Array || typeof value === 'object') {
|
|
|
|
recursivePromiseApply(value, promises, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return first && Promise.all(promises).then(() => object);
|
|
|
|
}
|
|
|
|
|
2017-11-06 12:10:13 +01:00
|
|
|
export function asyncIterator(array, callback) {
|
2017-11-06 12:07:36 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const results = [];
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
function loop() {
|
|
|
|
if (i === array.length) return resolve(results);
|
|
|
|
|
|
|
|
const item = array[i];
|
|
|
|
callback(item, i, array).then(result => {
|
|
|
|
results.push(result);
|
|
|
|
i ++;
|
|
|
|
loop();
|
|
|
|
}).catch(reject);
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
});
|
2017-10-24 12:33:14 +02:00
|
|
|
}
|
2017-11-06 12:07:36 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
// }
|
2017-11-08 22:11:42 +01:00
|
|
|
|
2017-11-08 23:39:58 +01:00
|
|
|
export function createThrottle() {
|
2017-11-08 22:11:42 +01:00
|
|
|
let next = null;
|
|
|
|
|
|
|
|
return callback => {
|
|
|
|
const startLoop = next === null;
|
|
|
|
next = callback;
|
|
|
|
|
2017-11-08 23:34:54 +01:00
|
|
|
if (!startLoop) return null;
|
2017-11-08 22:11:42 +01:00
|
|
|
|
2017-11-08 23:34:54 +01:00
|
|
|
return function loop() {
|
|
|
|
const promise = next().then(() => {
|
2017-11-08 22:11:42 +01:00
|
|
|
if (typeof next === 'function') return loop();
|
|
|
|
});
|
|
|
|
next = true;
|
2017-11-08 23:34:54 +01:00
|
|
|
return promise;
|
|
|
|
}().then(() => {
|
2017-11-08 23:27:40 +01:00
|
|
|
next = null;
|
|
|
|
});
|
2017-11-08 22:11:42 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// export function createThrottle() {
|
|
|
|
// let next = null;
|
|
|
|
//
|
|
|
|
// return async callback => {
|
|
|
|
// const startLoop = next === null;
|
|
|
|
// next = callback;
|
|
|
|
//
|
|
|
|
// if (!startLoop) return;
|
|
|
|
//
|
|
|
|
// while (typeof next === 'function') {
|
|
|
|
// callback = next;
|
|
|
|
// next = true;
|
|
|
|
// await callback();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// next = null;
|
|
|
|
// };
|
|
|
|
// }
|