-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheFor.js
More file actions
28 lines (27 loc) · 802 Bytes
/
eFor.js
File metadata and controls
28 lines (27 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function eForStep(arr, arrLen, i, callback) {
if (i < arrLen) {
let item = arr[i];
callback(true, item);
setTimeout(eForStep.bind(this, arr, arrLen, i + 1, callback), 0);
} else {
callback(false);
}
}
/**
* For statement emulation to work with large arrays without event loop blocking.
*
* eFor(arr, function (pending, item) {
* if (pending) {
* // do something with item
* } else {
* // do something after cycle end
* }
* });
*
* @param {Array} arr на вход получаем массив
* @param {Function} callback функция для выполнения с каждым элементом массива
*/
function eFor(arr, callback) {
eForStep(arr, arr.length, 0, callback);
}
export {eFor, eForStep};