@azteam/util
Advanced tools
Comparing version 1.0.37 to 1.0.38
@@ -200,6 +200,10 @@ "use strict"; | ||
_asyncLoop = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(data, asyncFunction) { | ||
var isLogProcessing, | ||
var options, | ||
isLogProcessing, | ||
reverse, | ||
length, | ||
i, | ||
result, | ||
_i, | ||
_result, | ||
_args2 = arguments; | ||
@@ -210,28 +214,32 @@ return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
case 0: | ||
isLogProcessing = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : false; | ||
length = data.length; | ||
options = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {}; | ||
isLogProcessing = options.isLogProcessing, reverse = options.reverse, length = data.length; | ||
if (!length) { | ||
_context2.next = 18; | ||
_context2.next = 35; | ||
break; | ||
} | ||
i = 0; | ||
case 4: | ||
if (!(i < length)) { | ||
_context2.next = 17; | ||
if (!reverse) { | ||
_context2.next = 20; | ||
break; | ||
} | ||
i = length - 1; | ||
case 5: | ||
if (!(i >= 0)) { | ||
_context2.next = 18; | ||
break; | ||
} | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('run', "(".concat(i + 1, "/").concat(length, ")")); | ||
console.log('run reverse', "(".concat(i + 1, "/").concat(length, ")")); | ||
} | ||
_context2.next = 8; | ||
_context2.next = 9; | ||
return asyncFunction(data[i], i); | ||
case 8: | ||
case 9: | ||
result = _context2.sent; | ||
if (!(typeof result !== 'undefined')) { | ||
_context2.next = 11; | ||
_context2.next = 12; | ||
break; | ||
} | ||
return _context2.abrupt("return", result); | ||
case 11: | ||
case 12: | ||
if (isLogProcessing) { | ||
@@ -241,13 +249,47 @@ // eslint-disable-next-line no-console | ||
} | ||
_context2.next = 14; | ||
_context2.next = 15; | ||
return timeout(10); | ||
case 14: | ||
i += 1; | ||
_context2.next = 4; | ||
case 15: | ||
i -= 1; | ||
_context2.next = 5; | ||
break; | ||
case 17: | ||
case 18: | ||
_context2.next = 34; | ||
break; | ||
case 20: | ||
_i = 0; | ||
case 21: | ||
if (!(_i < length)) { | ||
_context2.next = 34; | ||
break; | ||
} | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('run', "(".concat(_i + 1, "/").concat(length, ")")); | ||
} | ||
_context2.next = 25; | ||
return asyncFunction(data[_i], _i); | ||
case 25: | ||
_result = _context2.sent; | ||
if (!(typeof _result !== 'undefined')) { | ||
_context2.next = 28; | ||
break; | ||
} | ||
return _context2.abrupt("return", _result); | ||
case 28: | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('done', "(".concat(_i + 1, "/").concat(length, ")")); | ||
} | ||
_context2.next = 31; | ||
return timeout(10); | ||
case 31: | ||
_i += 1; | ||
_context2.next = 21; | ||
break; | ||
case 34: | ||
return _context2.abrupt("return", true); | ||
case 18: | ||
case 35: | ||
return _context2.abrupt("return", false); | ||
case 19: | ||
case 36: | ||
case "end": | ||
@@ -254,0 +296,0 @@ return _context2.stop(); |
{ | ||
"name": "@azteam/util", | ||
"version": "1.0.37", | ||
"version": "1.0.38", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -225,23 +225,45 @@ import _ from 'lodash'; | ||
export async function asyncLoop(data, asyncFunction, isLogProcessing = false) { | ||
const {length} = data; | ||
export async function asyncLoop(data, asyncFunction, options = {}) { | ||
const {isLogProcessing, reverse} = options, | ||
{length} = data; | ||
if (length) { | ||
for (let i = 0; i < length; i += 1) { | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('run', `(${i + 1}/${length})`); | ||
} | ||
if (reverse) { | ||
for (let i = length - 1; i >= 0; i -= 1) { | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('run reverse', `(${i + 1}/${length})`); | ||
} | ||
const result = await asyncFunction(data[i], i); | ||
if (typeof result !== 'undefined') { | ||
return result; | ||
const result = await asyncFunction(data[i], i); | ||
if (typeof result !== 'undefined') { | ||
return result; | ||
} | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('done', `(${i + 1}/${length})`); | ||
} | ||
await timeout(10); // free blocking | ||
} | ||
} else { | ||
for (let i = 0; i < length; i += 1) { | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('run', `(${i + 1}/${length})`); | ||
} | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('done', `(${i + 1}/${length})`); | ||
const result = await asyncFunction(data[i], i); | ||
if (typeof result !== 'undefined') { | ||
return result; | ||
} | ||
if (isLogProcessing) { | ||
// eslint-disable-next-line no-console | ||
console.log('done', `(${i + 1}/${length})`); | ||
} | ||
await timeout(10); // free blocking | ||
} | ||
await timeout(10); // free blocking | ||
} | ||
return true; | ||
@@ -248,0 +270,0 @@ } |
34002
590