add throttle function

This commit is contained in:
casperlamboo 2017-11-08 22:11:42 +01:00
parent e93a497cd8
commit 8eb1e4e7d0

View File

@ -42,3 +42,42 @@ export function asyncIterator(array, callback) {
// }
// return result;
// }
export function createThrottle() {
let next = null;
return callback => {
const startLoop = next === null;
next = callback;
if (!startLoop) return;
return (function loop() {
return next().then(() => {
if (typeof next === 'function') return loop();
});
next = true;
})();
next = null;
};
}
// 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;
// };
// }