async-to-sync
Version:
async-to-sync is help you for Javascript asynchronous function to synchronous function.
100 lines (78 loc) • 2.93 kB
JavaScript
;
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var ats = function ats(arrCallback, fallback) {
var promise = function promise(cb) {
return new Promise(function (res) {
return cb(res);
});
};
_asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cb;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 4;
_iterator = arrCallback[Symbol.iterator]();
case 6:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context.next = 13;
break;
}
cb = _step.value;
_context.next = 10;
return promise(cb);
case 10:
_iteratorNormalCompletion = true;
_context.next = 6;
break;
case 13:
_context.next = 19;
break;
case 15:
_context.prev = 15;
_context.t0 = _context['catch'](4);
_didIteratorError = true;
_iteratorError = _context.t0;
case 19:
_context.prev = 19;
_context.prev = 20;
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
case 22:
_context.prev = 22;
if (!_didIteratorError) {
_context.next = 25;
break;
}
throw _iteratorError;
case 25:
return _context.finish(22);
case 26:
return _context.finish(19);
case 27:
_context.next = 34;
break;
case 29:
_context.prev = 29;
_context.t1 = _context['catch'](0);
if (!(typeof fallback !== 'function')) {
_context.next = 33;
break;
}
return _context.abrupt('return', console.error('Error:', _context.t1));
case 33:
fallback();
case 34:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 29], [4, 15, 19, 27], [20,, 22, 26]]);
}))();
};