cancelable-promise
Advanced tools
Comparing version 1.1.0 to 2.1.0
@@ -19,36 +19,29 @@ export default class CancelablePromise { | ||
this._canceled = false; | ||
this._onError = []; | ||
this._onSuccess = []; | ||
let success = (...args) => { | ||
if(this._canceled) return; | ||
this.then = this._promise.then.bind(this._promise); | ||
this._onSuccess.forEach((cb) => { | ||
cb(...args); | ||
}); | ||
}; | ||
let error = (...args) => { | ||
if(this._canceled) return; | ||
this.then = this._promise.then.bind(this._promise); | ||
this._onError.forEach((cb) => { | ||
cb(...args); | ||
}); | ||
}; | ||
this._promise.then(success, error); | ||
} | ||
then(success, error) { | ||
if (success) this._onSuccess.push(success); | ||
if (error) this._onError.push(error); | ||
return this; | ||
const p = new CancelablePromise((resolve, reject) => { | ||
this._promise.then((r) => { | ||
if (this._canceled) { | ||
p.cancel(); | ||
resolve(); | ||
} | ||
if (success && !this._canceled) { | ||
resolve(success(r)); | ||
} | ||
}, (r) => { | ||
if (this._canceled) { | ||
p.cancel(); | ||
reject(); | ||
} | ||
if (error && !this._canceled) { | ||
reject(error(r)); | ||
} | ||
}); | ||
}); | ||
return p; | ||
} | ||
catch(error) { | ||
if (error) this._onError.push(error); | ||
return this; | ||
return this.then(undefined, error); | ||
} | ||
@@ -58,3 +51,4 @@ | ||
this._canceled = true; | ||
return this; | ||
} | ||
} |
@@ -7,7 +7,7 @@ "use strict"; | ||
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; }; })(); | ||
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var CancelablePromise = (function () { | ||
var CancelablePromise = function () { | ||
_createClass(CancelablePromise, null, [{ | ||
@@ -36,42 +36,9 @@ key: "all", | ||
function CancelablePromise(executor) { | ||
var _this = this; | ||
_classCallCheck(this, CancelablePromise); | ||
var superResolve = undefined, | ||
superReject = undefined; | ||
var superResolve = void 0, | ||
superReject = void 0; | ||
this._promise = new Promise(executor); | ||
this._canceled = false; | ||
this._onError = []; | ||
this._onSuccess = []; | ||
var success = function success() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (_this._canceled) return; | ||
_this.then = _this._promise.then.bind(_this._promise); | ||
_this._onSuccess.forEach(function (cb) { | ||
cb.apply(undefined, args); | ||
}); | ||
}; | ||
var error = function error() { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
if (_this._canceled) return; | ||
_this.then = _this._promise.then.bind(_this._promise); | ||
_this._onError.forEach(function (cb) { | ||
cb.apply(undefined, args); | ||
}); | ||
}; | ||
this._promise.then(success, error); | ||
} | ||
@@ -82,5 +49,24 @@ | ||
value: function then(success, error) { | ||
if (success) this._onSuccess.push(success); | ||
if (error) this._onError.push(error); | ||
return this; | ||
var _this = this; | ||
var p = new CancelablePromise(function (resolve, reject) { | ||
_this._promise.then(function (r) { | ||
if (_this._canceled) { | ||
p.cancel(); | ||
resolve(); | ||
} | ||
if (success && !_this._canceled) { | ||
resolve(success(r)); | ||
} | ||
}, function (r) { | ||
if (_this._canceled) { | ||
p.cancel(); | ||
reject(); | ||
} | ||
if (error && !_this._canceled) { | ||
reject(error(r)); | ||
} | ||
}); | ||
}); | ||
return p; | ||
} | ||
@@ -90,4 +76,3 @@ }, { | ||
value: function _catch(error) { | ||
if (error) this._onError.push(error); | ||
return this; | ||
return this.then(undefined, error); | ||
} | ||
@@ -98,2 +83,3 @@ }, { | ||
this._canceled = true; | ||
return this; | ||
} | ||
@@ -103,5 +89,4 @@ }]); | ||
return CancelablePromise; | ||
})(); | ||
}(); | ||
exports["default"] = CancelablePromise; | ||
module.exports = exports["default"]; | ||
exports.default = CancelablePromise; |
{ | ||
"name": "cancelable-promise", | ||
"version": "1.1.0", | ||
"version": "2.1.0", | ||
"description": "A simple cancelable promise", | ||
"main": "dist/CancelablePromise.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"compile": "node ./node_modules/babel/bin/babel.js -d dist/ CancelablePromise.js", | ||
"prepublish": "npm run compile" | ||
"test": "mocha --opts mocha.opts", | ||
"build": "babel -d dist/ CancelablePromise.js", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -25,6 +25,15 @@ "repository": { | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015" | ||
] | ||
}, | ||
"homepage": "https://github.com/alkemics/CancelablePromise#readme", | ||
"devDependencies": { | ||
"babel": "~5.8.29" | ||
"babel-cli": "~6.16.0", | ||
"babel-core": "~6.17.0", | ||
"babel-preset-es2015": "~6.16.0", | ||
"chai": "~3.5.0", | ||
"mocha": "~3.1.2" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
26305
8
237
0
5
1