attempt-man
Version:
Beautifully manage attempts for your process!
88 lines (64 loc) • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
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"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Attempt = function () {
/**
* Create a new Attempt.
*/
function Attempt() {
_classCallCheck(this, Attempt);
this.error = null;
this.started = false;
this.successful = false;
}
/**
* Run the attempt.
*/
_createClass(Attempt, [{
key: 'run',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(callback) {
var result;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!this.started) {
_context.next = 2;
break;
}
throw new Error('This attempt has already started');
case 2:
this.started = true;
_context.prev = 3;
_context.next = 6;
return callback();
case 6:
result = _context.sent;
this.successful = true;
return _context.abrupt('return', result);
case 11:
_context.prev = 11;
_context.t0 = _context['catch'](3);
this.error = _context.t0;
case 14:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[3, 11]]);
}));
function run(_x) {
return _ref.apply(this, arguments);
}
return run;
}()
}]);
return Attempt;
}();
exports.default = Attempt;
module.exports = exports['default'];