Created
December 11, 2017 00:30
-
-
Save lesha-co/c347b2af1c2ccbb16f9d41c253722e47 to your computer and use it in GitHub Desktop.
Хелпер, который последовательно выполняет асинхронный executor над элементами из list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Хелпер, который последовательно выполняет асинхронный executor над элементами из list | |
* @param {Array} list список параметров для каждого выполнения | |
* @param {Function} executor асинхронная (возвращает Promise) или синхронная функция | |
*/ | |
function asyncLoop(list, executor) { | |
return new Promise((resolve, reject) => { | |
let results = []; | |
(function loop(i) { | |
if (i < list.length) { | |
return new Promise((resolve, reject) => { | |
let r = executor(list[i]); | |
if (r instanceof Promise) return r.then(resolve); | |
else resolve(r); | |
}).then((result) => { | |
results.push(result); | |
return loop(i + 1); | |
}); | |
} else resolve(results); | |
})(0); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment