promise-toolbox
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.try = exports.attempt = exports.TimeoutError = exports.promisifyAll = exports.promisify = exports.finally = exports.fromCallback = exports.forOwn = exports.forIterable = exports.forIn = exports.forEach = exports.forArray = exports.makeAsyncIterator = exports.defer = exports.cancelable = exports.cancellable = exports.CancelToken = exports.Cancel = exports.nodeify = exports.isPromise = undefined; | ||
exports.try = exports.attempt = exports.TimeoutError = exports.promisifyAll = exports.promisify = exports.finally = exports.fromCallback = exports.forOwn = exports.forIterable = exports.forIn = exports.forEach = exports.forArray = exports.makeAsyncIterator = exports.defer = exports.cancellable = exports.cancelable = exports.CancelToken = exports.Cancel = exports.nodeify = exports.wrapApply = exports.wrapCall = exports.isPromise = undefined; | ||
@@ -162,5 +162,5 @@ 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 _wrapCall = function _wrapCall(fn, args, thisArg) { | ||
var wrapCall = exports.wrapCall = function wrapCall(fn, arg, thisArg) { | ||
try { | ||
return _wrap(fn.call(thisArg, args)); | ||
return _wrap(fn.call(thisArg, arg)); | ||
} catch (error) { | ||
@@ -171,3 +171,3 @@ return Promise.reject(error); | ||
var _wrapApply = function _wrapApply(fn, args, thisArg) { | ||
var wrapApply = exports.wrapApply = function wrapApply(fn, args, thisArg) { | ||
try { | ||
@@ -311,18 +311,18 @@ return _wrap(fn.apply(thisArg, args)); | ||
key: 'race', | ||
value: function race(tokens) { | ||
var _CancelToken$source = CancelToken.source(), | ||
cancel = _CancelToken$source.cancel, | ||
token = _CancelToken$source.token; | ||
value: function race(tokens, _executor) { | ||
return new CancelToken(function (cancel) { | ||
_forEach(tokens, function (token) { | ||
var reason = token.reason; | ||
_forEach(tokens, function (token) { | ||
var reason = token.reason; | ||
if (reason !== undefined) { | ||
cancel(reason); | ||
return false; | ||
} | ||
if (reason) { | ||
cancel(reason); | ||
return false; | ||
(token._listeners || (token._listeners = [])).push(cancel); | ||
}); | ||
if (_executor !== undefined) { | ||
_executor(cancel); | ||
} | ||
(token._listeners || (token._listeners = [])).push(cancel); | ||
}); | ||
return token; | ||
} | ||
@@ -371,13 +371,10 @@ }]); | ||
if (executor !== undefined) { | ||
return CancelToken.race([this, new CancelToken(executor)]); | ||
return CancelToken.race([this], executor); | ||
} | ||
var _CancelToken$source2 = CancelToken.source(), | ||
cancel = _CancelToken$source2.cancel, | ||
token = _CancelToken$source2.token; | ||
return { | ||
cancel, | ||
token: CancelToken.race([this, token]) | ||
}; | ||
var cancel = void 0; | ||
var token = CancelToken.race([this], function (c) { | ||
cancel = c; | ||
}); | ||
return { cancel, token }; | ||
} | ||
@@ -405,3 +402,3 @@ }, { | ||
var reason = this._reason; | ||
promise = this._promise = reason ? Promise.resolve(reason) : new Promise(function (resolve) { | ||
promise = this._promise = reason !== undefined ? Promise.resolve(reason) : new Promise(function (resolve) { | ||
_this2._resolve = resolve; | ||
@@ -429,3 +426,3 @@ }); | ||
// | ||
// @cancellable | ||
// @cancelable | ||
// async fn (cancelToken, other, args) { | ||
@@ -448,6 +445,6 @@ // if (!cancelToken.requested) { | ||
var cancellable = exports.cancellable = function cancellable(target, name, descriptor) { | ||
var cancelable = exports.cancelable = function cancelable(target, name, descriptor) { | ||
var fn = descriptor !== undefined ? descriptor.value : target; | ||
function cancellableWrapper() { | ||
function cancelableWrapper() { | ||
var length = arguments.length; | ||
@@ -459,5 +456,5 @@ | ||
var _CancelToken$source3 = CancelToken.source(), | ||
cancel = _CancelToken$source3.cancel, | ||
token = _CancelToken$source3.token; | ||
var _CancelToken$source = CancelToken.source(), | ||
cancel = _CancelToken$source.cancel, | ||
token = _CancelToken$source.token; | ||
@@ -477,9 +474,9 @@ var args = new Array(length + 1); | ||
if (descriptor !== undefined) { | ||
descriptor.value = cancellableWrapper; | ||
descriptor.value = cancelableWrapper; | ||
return descriptor; | ||
} | ||
return cancellableWrapper; | ||
return cancelableWrapper; | ||
}; | ||
exports.cancelable = cancellable; // alternative US spelling | ||
exports.cancellable = cancelable; // alternative UK spelling | ||
@@ -535,3 +532,3 @@ // ------------------------------------------------------------------- | ||
var predicates = void 0; | ||
if (n) { | ||
if (n !== 0) { | ||
predicates = new Array(n); | ||
@@ -544,3 +541,3 @@ for (var i = 0; i < n; ++i) { | ||
return _wrap(this).then(null, function (reason) { | ||
if (predicates) { | ||
if (predicates !== undefined) { | ||
for (var _i = 0; _i < n; ++_i) { | ||
@@ -653,3 +650,3 @@ if (_matchError(predicates[_i], reason)) { | ||
resource.p.then(function (value) { | ||
return _wrapCall(d, value).then(onSettle, onFailure); | ||
return wrapCall(d, value).then(onSettle, onFailure); | ||
}, onSettle); | ||
@@ -687,3 +684,3 @@ | ||
var onSettle = function onSettle() { | ||
return (spread ? _wrapApply : _wrapCall)(handler, values, _this5).then(function (value) { | ||
return (spread ? wrapApply : wrapCall)(handler, values, _this5).then(function (value) { | ||
return dispose(resolve, value); | ||
@@ -1082,3 +1079,3 @@ }, function (reason) { | ||
_wrapApply(fn, args, this).then(function (result) { | ||
wrapApply(fn, args, this).then(function (result) { | ||
return cb(null, result); | ||
@@ -1085,0 +1082,0 @@ }, function (reason) { |
@@ -1,2 +0,2 @@ | ||
(function(b){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=b();else if("function"==typeof define&&define.amd)define([],b);else{var c;c="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,c.promiseToolbox=b()}})(function(){return function j(k,m,q){function v(z,A){if(!m[z]){if(!k[z]){var B="function"==typeof require&&require;if(!A&&B)return B(z,!0);if(w)return w(z,!0);var C=new Error("Cannot find module '"+z+"'");throw C.code="MODULE_NOT_FOUND",C}var D=m[z]={exports:{}};k[z][0].call(D.exports,function(E){var F=k[z][1][E];return v(F?F:E)},D,D.exports,j,k,m,q)}return m[z].exports}for(var w="function"==typeof require&&require,y=0;y<q.length;y++)v(q[y]);return v}({1:[function(j,k,m){"use strict";function q(La,Ma){if(!La)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return Ma&&("object"==typeof Ma||"function"==typeof Ma)?Ma:La}function v(La,Ma){if("function"!=typeof Ma&&null!==Ma)throw new TypeError("Super expression must either be null or a function, not "+typeof Ma);La.prototype=Object.create(Ma&&Ma.prototype,{constructor:{value:La,enumerable:!1,writable:!0,configurable:!0}}),Ma&&(Object.setPrototypeOf?Object.setPrototypeOf(La,Ma):La.__proto__=Ma)}function w(La,Ma){if(!(La instanceof Ma))throw new TypeError("Cannot call a class as a function")}function y(La){return fa(this).then(function(Ma){return ia(Ma,La)})}function z(La){return La&&this.then(function(Ma){return La(null,Ma)},function(Ma){return La(Ma)}).then(null,W),this}function C(La,Ma){this.d=Ma,this.p=La}function G(La){var Ma=this;return fa(this).then(function(){return fa(La()).then(function(){return Ma})},function(){return fa(La()).then(function(){return Ma})})}function H(){return fa(this).then(Ga,Ha)}Object.defineProperty(m,"__esModule",{value:!0}),m.try=m.attempt=m.TimeoutError=m.promisifyAll=m.promisify=m.finally=m.fromCallback=m.forOwn=m.forIterable=m.forIn=m.forEach=m.forArray=m.makeAsyncIterator=m.defer=m.cancelable=m.cancellable=m.CancelToken=m.Cancel=m.nodeify=m.isPromise=void 0;var N=function(){function La(Ma,Na){for(var Pa,Oa=0;Oa<Na.length;Oa++)Pa=Na[Oa],Pa.enumerable=Pa.enumerable||!1,Pa.configurable=!0,"value"in Pa&&(Pa.writable=!0),Object.defineProperty(Ma,Pa.key,Pa)}return function(Ma,Na,Oa){return Na&&La(Ma.prototype,Na),Oa&&La(Ma,Oa),Ma}}();m.all=y,m.asCallback=z,m.catchPlus=function(){var La=this,Ma=arguments.length-1,Na;if(0>Ma||"function"!=typeof(Na=arguments[Ma]))return this;var Oa;if(Ma){Oa=Array(Ma);for(var Pa=0;Pa<Ma;++Pa)Oa[Pa]=arguments[Pa]}return fa(this).then(null,function(Qa){if(Oa){for(var Ra=0;Ra<Ma;++Ra)if(ra(Oa[Ra],Qa))return Na(Qa);}else if(oa(Qa))return Na(Qa);return La})},m.delay=function(La){var Ma=this;return da(this)?this.then(function(Na){return new Promise(function(Oa){setTimeout(function(){return Oa(Na)},La)})}):new Promise(function(Na){setTimeout(function(){return Na(Ma)},La)})},m.disposer=function(La){return new C(this,La)},m.using=function(){var La=this,Ma=arguments.length-1;if(1>Ma)throw new TypeError("using expects at least 2 arguments");var Na=arguments[Ma],Oa=arguments[0],Pa=1<Ma||!Q(Oa);if(Pa){Oa=Array(Ma);for(var Qa=0;Qa<Ma;++Qa)Oa[Qa]=arguments[Qa]}else Ma=Oa.length;var Ra=V(function(Sa,Ta){var Ua=Ma,Va=function(){0==--Ua&&Sa(Ta)},Wa=function(Ya){setTimeout(function(){throw Ya},0)};X(Oa,function(Xa){var Ya;null!=Xa&&"function"==typeof(Ya=Xa.d)?(Xa.p.then(function(Za){return ga(Ya,Za).then(Va,Wa)},Va),Xa.p=Xa.d=null):--Ua})});return new Promise(function(Sa,Ta){var Ua=Array(Ma),Va=Ma,Wa=function(_a){Wa=Ya,Za=function(){return Ra(Ta,_a)},Ya()},Xa=function(_a){return Wa(_a)},Ya=function(){0==--Va&&Za()},Za=function(){return(Pa?ha:ga)(Na,Ua,La).then(function(_a){return Ra(Sa,_a)},function(_a){return Ra(Ta,_a)})};X(Oa,function($a,_a){var ab=$a instanceof C?$a.p:$a;return null===ab?void Xa(new TypeError("resource has already been disposed of")):void ab.then(function(bb){Ua[_a]=bb,Ya()},Xa)})})},m.join=function(){var La=arguments.length-1,Ma=arguments[La],Na;if(0==La)return new Promise(function(Pa){return Pa(Ma())});if(1!=La){Na=Array(La);for(var Oa=0;Oa<La;++Oa)Na[Oa]=arguments[Oa]}else if(!S(Na=arguments[0]))return fa(Na).then(function(Pa){return Ma(Pa)});return ia(Na).then(function(Pa){return Ma.apply(null,Pa)})},m.lastly=G,m.reflect=H,m.settle=function(){return y.call(this,function(La){return H.call(La)})},m.some=function(La){return fa(this).then(function(Ma){return Ia(Ma,La)})},m.tap=function(La,Ma){var Na=this,Oa=function(){return Na};return fa(this).then(La&&function(Pa){return fa(La(Pa)).then(Oa)},Ma&&function(Pa){return fa(Ma(Pa)).then(Oa)})},m.timeout=function(La,Ma){var Na=this;return new Promise(function(Oa,Pa){var Qa=setTimeout(function(){if(Qa=null,"function"==typeof Na.cancel&&Na.cancel(),Ma)try{Oa(Ma())}catch(Ra){Pa(Ra)}else Pa(new Ja)},La);fa(Na).then(function(Ra){null!==Qa&&clearTimeout(Qa),Oa(Ra)},function(Ra){null!==Qa&&clearTimeout(Qa),Pa(Ra)})})},m.unpromisify=function(){var La=this;return Aa(function(){var Ma=arguments.length-1,Na;if(0>Ma||"function"!=typeof(Na=arguments[Ma]))throw new Error("missing callback");for(var Oa=Array(Ma),Pa=0;Pa<Ma;++Pa)Oa[Pa]=arguments[Pa];ha(La,Oa,this).then(function(Qa){return Na(null,Qa)},function(Qa){return Na(Qa)})},La.name,La.length+1)};var O=j("make-error");if("function"!=typeof Promise||"function"!=typeof Promise.reject||"function"!=typeof Promise.resolve)throw new Error("a standard Promise implementation is required (https://github.com/JsCommunity/promise-toolbox#usage)");var P=function(Ma,Na){var Oa=2<arguments.length&&void 0!==arguments[2]?arguments[2]:Ma.length;return Oa-=Na.length,0<=Oa&&Ma.indexOf(Na,Oa)===Oa},Q=Array.isArray||function(La){return La instanceof Array},R=function(Ma){return"number"==typeof Ma&&0<=Ma&&Ma<Infinity&&Math.floor(Ma)===Ma},S=function(Ma){return Ma&&"function"!=typeof Ma&&R(Ma.length)},T="function"==typeof Symbol&&Symbol.iterator||"@@iterator",U=function(Ma){return Ma&&"function"==typeof Ma[T]},V=function(Ma){var Na;return function(){return Ma&&(Na=Ma.apply(this,arguments),Ma=null),Na}},W=function(){},X=function(Ma,Na){for(var Oa=Ma.length,Pa=0;Pa<Oa;++Pa)Na(Ma[Pa],Pa,Ma)},Y=function(Ma,Na){for(var Oa in Ma)Na(Ma[Oa],Oa,Ma)},Z=function(Ma,Na){for(var Pa,Oa=Ma[Symbol.iterator]();!(Pa=Oa.next()).done;)Na(Pa.value,null,Ma)},$=Object.prototype.hasOwnProperty,aa=function(Ma,Na){for(var Oa in Ma)$.call(Ma,Oa)&&Na(Ma[Oa],Oa,Ma)},ba=function(Ma,Na){return Q(Ma)?X(Ma,Na):U(Ma)?Z(Ma,Na):S(Ma)?X(Ma,Na):aa(Ma,Na)},ca=function(Ma,Na){var Oa=S(Ma)?Array(Ma.length):{};return Na&&ba(Ma,function(Pa,Qa){Oa[Qa]=Na(Pa,Qa,Ma)}),Oa},da=m.isPromise=function(Ma){return null!=Ma&&"function"==typeof Ma.then},ea=function(Ma){return function(Na,Oa){var Pa=Promise.resolve();return Ma(Na,function(Qa,Ra){Pa=da(Qa)?Pa.then(function(){return Qa.then(function(Sa){return Oa(Sa,Ra,Na)})}):Pa.then(function(){return Oa(Qa,Ra,Na)})}),Pa}},fa=function(Ma){return da(Ma)?Ma:Promise.resolve(Ma)},ga=function(Ma,Na,Oa){try{return fa(Ma.call(Oa,Na))}catch(Pa){return Promise.reject(Pa)}},ha=function(Ma,Na,Oa){try{return fa(Ma.apply(Oa,Na))}catch(Pa){return Promise.reject(Pa)}},ia=function(Ma,Na){return new Promise(function(Oa,Pa){var Qa=ca(Ma,Na),Ra=1,Sa=function(){if(0==--Ra){var Wa=Qa;Qa=null,Oa(Wa)}},Ta=function(Wa,Xa){Qa&&(Qa[Xa]=Wa,Sa())},Ua=function(Wa){Qa&&(Qa=null,Pa(Wa))};ba(Na?Qa:Ma,function(Va,Wa){++Ra,da(Va)?Va.then(function(Xa){return Ta(Xa,Wa)},Ua):Ta(Va,Wa)}),Sa()})};m.nodeify=z;var ja=m.Cancel=function(){function La(){var Ma=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";w(this,La),Object.defineProperty(this,"message",{enumerable:!0,value:Ma})}return N(La,[{key:"toString",value:function(){return`Cancel: ${this._message}`}}]),La}(),ka="CancelToken",la="function"==typeof Symbol&&Symbol.toStringTag||"@@toStringTag",ma=m.CancelToken=function(){function La(Ma){var Na=this;w(this,La),this._listeners=null,this._promise=null,this._reason=void 0,this._resolve=null;var Oa=function(Ra){Oa=W;var Sa=Na._reason=Ra instanceof ja?Ra:new ja(Ra),Ta=Na._resolve;null!==Ta&&(Na._resolve=null,Ta(Sa));var Ua=Na._listeners;null!==Ua&&(Na._listeners=null,X(Ua,function(Va){Va(Sa)}))};Ma(function(Ra){return Oa(Ra)})}return N(La,null,[{key:"isCancelToken",value:function(Na){return null!=Na&&"function"==typeof Na[la]&&Na[la]()===ka}},{key:"source",value:function(){var Na,Oa=new La(function(Pa){Na=Pa});return{cancel:Na,token:Oa}}},{key:"race",value:function(Na){var Oa=La.source(),Pa=Oa.cancel,Qa=Oa.token;return ba(Na,function(Ra){var Sa=Ra.reason;return Sa?(Pa(Sa),!1):void(Ra._listeners||(Ra._listeners=[])).push(Pa)}),Qa}}]),N(La,[{key:"fork",value:function(Na){if(void 0!==Na)return La.race([this,new La(Na)]);var Oa=La.source(),Pa=Oa.cancel,Qa=Oa.token;return{cancel:Pa,token:La.race([this,Qa])}}},{key:"throwIfRequested",value:function(){var Na=this._reason;if(void 0!==Na)throw Na}},{key:la,value:function(){return ka}},{key:"promise",get:function(){var Na=this,Oa=this._promise;if(null===Oa){var Pa=this._reason;Oa=this._promise=Pa?Promise.resolve(Pa):new Promise(function(Qa){Na._resolve=Qa})}return Oa}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),La}(),na=m.cancellable=function(Ma,Na,Oa){function Pa(){var Ra=arguments.length;if(0!==Ra&&ma.isCancelToken(arguments[0]))return Qa.apply(this,arguments);var Sa=ma.source(),Ta=Sa.cancel,Ua=Sa.token,Va=Array(Ra+1);Va[0]=Ua;for(var Wa=0;Wa<Ra;++Wa)Va[Wa+1]=arguments[Wa];var Xa=Qa.apply(this,Va);return Xa.cancel=Ta,Xa}var Qa=Oa===void 0?Ma:Oa.value;return void 0===Oa?Pa:(Oa.value=Pa,Oa)};m.cancelable=na;var oa=function(Ma){return!(Ma instanceof ReferenceError||Ma instanceof SyntaxError||Ma instanceof TypeError)},pa={error:null},qa=function(Ma){try{return Ma()}catch(Na){return pa.error=Na,pa}},ra=function(Ma,Na){return"function"==typeof Ma?Ma.prototype instanceof Error?Na instanceof Ma:Ma(Na):"object"==typeof Ma?null!=Na&&qa(function(){return aa(Ma,function(Oa,Pa){if(Na[Pa]!==Oa)throw null})})!==pa:void 0},sa=m.defer=function(){var Ma,Na,Oa=new Promise(function(Pa,Qa){Ma=Pa,Na=Qa});return{promise:Oa,reject:Na,resolve:Ma}},ta=m.makeAsyncIterator=function(Ma){var Na=ea(Ma);return function(Oa){return fa(this).then(function(Pa){return Na(Pa,Oa)}).then(W)}},ua=m.forArray=ta(X),va=m.forEach=ta(ba),wa=m.forIn=ta(Y),xa=m.forIterable=ta(Z),ya=m.forOwn=ta(aa),za=m.fromCallback=function(Ma){return new Promise(function(Na,Oa){Ma(function(Pa,Qa){return Pa?Oa(Pa):Na(Qa)})})};m.finally=G;var Aa=function(){var La=Object.defineProperties;try{var Ma=La(function(){},{length:{value:2},name:{value:"foo"}});if(2===Ma.length&&"foo"===Ma.name)return function(Na,Oa,Pa){return La(Na,{length:{configurable:!0,value:Pa},name:{configurable:!0,value:Oa}})}}catch(Na){}return function(Na){return Na}}(),Ba=m.promisify=function(Ma,Na){return Aa(function(){for(var Oa=this,Pa=arguments.length,Qa=Array(Pa+1),Ra=0;Ra<Pa;++Ra)Qa[Ra]=arguments[Ra];return new Promise(function(Sa,Ta){Qa[Pa]=function(Ua,Va){return Ua?Ta(Ua):Sa(Va)},Ma.apply(Na||Oa,Qa)})},Ma.name,Ma.length&&Ma.length-1)},Ca=function(Ma){return!(P(Ma,"Sync")||P(Ma,"Async"))&&Ma},Da=m.promisifyAll=function(Ma){var Na=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},Oa=Na.mapper,Pa=void 0===Oa?Ca:Oa,Qa=Na.target,Ra=void 0===Qa?{}:Qa,Sa=Na.context,Ta=void 0===Sa?Ma:Sa;return Y(Ma,function(Ua,Va){var Wa;"function"==typeof Ua&&(Wa=Pa(Va,Ua,Ma))&&(Ra[Wa]=Ba(Ua,Ta))}),Ra},Ea=function(){return!1},Fa=function(){return!0},Ga=function(La){return function(Ma){return{__proto__:La,value:function(){return Ma}}}}({isFulfilled:Fa,isPending:Ea,isRejected:Ea,isResolved:Fa,reason:function(){throw new Error("no reason, the promise has resolved")}}),Ha=function(La){return function(Ma){return{__proto__:La,reason:function(){return Ma}}}}({isFulfilled:Ea,isPending:Ea,isRejected:Fa,isResolved:Ea,value:function(){throw new Error("no value, the promise has rejected")}}),Ia=function(Ma,Na){return new Promise(function(Oa,Pa){var Qa=[],Ra=[],Sa=function(Wa){Qa&&(Qa.push(Wa),0==--Na&&(Oa(Qa),Qa=Ra=null))},Ta=-Na,Ua=function(Wa){Qa&&(Ra.push(Wa),0==--Ta&&(Pa(Ra),Qa=Ra=null))};ba(Ma,function(Va){++Ta,fa(Va).then(Sa,Ua)})})},Ja=m.TimeoutError=function(La){function Ma(){return w(this,Ma),q(this,(Ma.__proto__||Object.getPrototypeOf(Ma)).call(this,"operation timed out"))}return v(Ma,La),Ma}(O.BaseError),Ka=m.attempt=function(Ma){return new Promise(function(Na){return Na(Ma())})};m.try=Ka},{"make-error":2}],2:[function(j,k,m){"use strict";function q(A){A&&w(this,"message",{configurable:!0,value:A,writable:!0});var B=this.constructor.name;B&&B!==this.name&&w(this,"name",{configurable:!0,value:B,writable:!0}),y(this,this.constructor)}var w=Object.defineProperty,y=Error.captureStackTrace;y||(y=function(B){var C=new Error;w(B,"stack",{configurable:!0,get:function(){var E=C.stack;return w(this,"stack",{value:E}),E},set:function(E){w(B,"stack",{configurable:!0,value:E,writable:!0})}})}),q.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:q,writable:!0}});var z=function(){function A(C,D){return w(C,"name",{configurable:!0,value:D})}try{var B=function(){};if(A(B,"foo"),"foo"===B.name)return A}catch(C){}}();m=k.exports=function(A,B){if(null==B||B===Error)B=q;else if("function"!=typeof B)throw new TypeError("super_ should be a function");var C;if("string"==typeof A)C=A,A=function(){B.apply(this,arguments)},z&&(z(A,C),C=null);else if("function"!=typeof A)throw new TypeError("constructor should be either a string or a function");A.super_=A["super"]=B;var D={constructor:{configurable:!0,value:A,writable:!0}};return null!=C&&(D.name={configurable:!0,value:C,writable:!0}),A.prototype=Object.create(B.prototype,D),A},m.BaseError=q},{}]},{},[1])(1)}); | ||
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.promiseToolbox=a()}})(function(){return function b(c,d,e){function a(h,i){if(!d[h]){if(!c[h]){var j="function"==typeof require&&require;if(!i&&j)return j(h,!0);if(g)return g(h,!0);var k=new Error("Cannot find module '"+h+"'");throw k.code="MODULE_NOT_FOUND",k}var f=d[h]={exports:{}};c[h][0].call(f.exports,function(b){var d=c[h][1][b];return a(d?d:b)},f,f.exports,b,c,d,e)}return d[h].exports}for(var g="function"==typeof require&&require,f=0;f<e.length;f++)a(e[f]);return a}({1:[function(a,b,c){"use strict";function d(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return b&&("object"==typeof b||"function"==typeof b)?b:a}function e(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}function f(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function g(a){return E(this).then(function(b){return H(b,a)})}function h(a){return a&&this.then(function(b){return a(null,b)},function(b){return a(b)}).then(null,u),this}function j(a,b){this.d=b,this.p=a}function i(a){var b=this;return E(this).then(function(){return E(a()).then(function(){return b})},function(){return E(a()).then(function(){return b})})}function k(){return E(this).then(da,ea)}Object.defineProperty(c,"__esModule",{value:!0}),c.try=c.attempt=c.TimeoutError=c.promisifyAll=c.promisify=c.finally=c.fromCallback=c.forOwn=c.forIterable=c.forIn=c.forEach=c.forArray=c.makeAsyncIterator=c.defer=c.cancellable=c.cancelable=c.CancelToken=c.Cancel=c.nodeify=c.wrapApply=c.wrapCall=c.isPromise=void 0;var l=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();c.all=g,c.asCallback=h,c.catchPlus=function(){var a,b=this,c=arguments.length-1;if(0>c||"function"!=typeof(a=arguments[c]))return this;var d;if(0!=c){d=Array(c);for(var e=0;e<c;++e)d[e]=arguments[e]}return E(this).then(null,function(e){if(d!==void 0){for(var f=0;f<c;++f)if(Q(d[f],e))return a(e);}else if(N(e))return a(e);return b})},c.delay=function(a){var b=this;return C(this)?this.then(function(b){return new Promise(function(c){setTimeout(function(){return c(b)},a)})}):new Promise(function(c){setTimeout(function(){return c(b)},a)})},c.disposer=function(a){return new j(this,a)},c.using=function(){var a=this,b=arguments.length-1;if(1>b)throw new TypeError("using expects at least 2 arguments");var c=arguments[b],d=arguments[0],e=1<b||!o(d);if(e){d=Array(b);for(var f=0;f<b;++f)d[f]=arguments[f]}else b=d.length;var g=t(function(a,c){var e=b,f=function(){0==--e&&a(c)},g=function(a){setTimeout(function(){throw a},0)};v(d,function(a){var b;null!=a&&"function"==typeof(b=a.d)?(a.p.then(function(a){return F(b,a).then(f,g)},f),a.p=a.d=null):--e})});return new Promise(function(f,h){var k=Array(b),i=b,l=function(a){l=n,o=function(){return g(h,a)},n()},m=function(a){return l(a)},n=function(){0==--i&&o()},o=function(){return(e?G:F)(c,k,a).then(function(a){return g(f,a)},function(a){return g(h,a)})};v(d,function(a,b){var c=a instanceof j?a.p:a;return null===c?void m(new TypeError("resource has already been disposed of")):void c.then(function(a){k[b]=a,n()},m)})})},c.join=function(){var a,b=arguments.length-1,c=arguments[b];if(0==b)return new Promise(function(a){return a(c())});if(1!=b){a=Array(b);for(var d=0;d<b;++d)a[d]=arguments[d]}else if(!q(a=arguments[0]))return E(a).then(function(a){return c(a)});return H(a).then(function(a){return c.apply(null,a)})},c.lastly=i,c.reflect=k,c.settle=function(){return g.call(this,function(a){return k.call(a)})},c.some=function(a){return E(this).then(function(b){return fa(b,a)})},c.tap=function(a,b){var c=this,d=function(){return c};return E(this).then(a&&function(b){return E(a(b)).then(d)},b&&function(a){return E(b(a)).then(d)})},c.timeout=function(a,b){var c=this;return new Promise(function(d,e){var f=setTimeout(function(){if(f=null,"function"==typeof c.cancel&&c.cancel(),b)try{d(b())}catch(a){e(a)}else e(new ga)},a);E(c).then(function(a){null!==f&&clearTimeout(f),d(a)},function(a){null!==f&&clearTimeout(f),e(a)})})},c.unpromisify=function(){var a=this;return Z(function(){var b,c=arguments.length-1;if(0>c||"function"!=typeof(b=arguments[c]))throw new Error("missing callback");for(var d=Array(c),e=0;e<c;++e)d[e]=arguments[e];G(a,d,this).then(function(a){return b(null,a)},function(a){return b(a)})},a.name,a.length+1)};var m=a("make-error");if("function"!=typeof Promise||"function"!=typeof Promise.reject||"function"!=typeof Promise.resolve)throw new Error("a standard Promise implementation is required (https://github.com/JsCommunity/promise-toolbox#usage)");var n=function(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:a.length;return c-=b.length,0<=c&&a.indexOf(b,c)===c},o=Array.isArray||function(a){return a instanceof Array},p=function(a){return"number"==typeof a&&0<=a&&a<Infinity&&Math.floor(a)===a},q=function(a){return a&&"function"!=typeof a&&p(a.length)},r="function"==typeof Symbol&&Symbol.iterator||"@@iterator",s=function(a){return a&&"function"==typeof a[r]},t=function(a){var b;return function(){return a&&(b=a.apply(this,arguments),a=null),b}},u=function(){},v=function(a,b){for(var c=a.length,d=0;d<c;++d)b(a[d],d,a)},w=function(a,b){for(var c in a)b(a[c],c,a)},x=function(a,b){for(var c,d=a[Symbol.iterator]();!(c=d.next()).done;)b(c.value,null,a)},y=Object.prototype.hasOwnProperty,z=function(a,b){for(var c in a)y.call(a,c)&&b(a[c],c,a)},A=function(a,b){return o(a)?v(a,b):s(a)?x(a,b):q(a)?v(a,b):z(a,b)},B=function(a,b){var c=q(a)?Array(a.length):{};return b&&A(a,function(d,e){c[e]=b(d,e,a)}),c},C=c.isPromise=function(a){return null!=a&&"function"==typeof a.then},D=function(a){return function(b,c){var d=Promise.resolve();return a(b,function(a,e){d=C(a)?d.then(function(){return a.then(function(a){return c(a,e,b)})}):d.then(function(){return c(a,e,b)})}),d}},E=function(a){return C(a)?a:Promise.resolve(a)},F=c.wrapCall=function(a,b,c){try{return E(a.call(c,b))}catch(a){return Promise.reject(a)}},G=c.wrapApply=function(a,b,c){try{return E(a.apply(c,b))}catch(a){return Promise.reject(a)}},H=function(a,b){return new Promise(function(c,d){var e=B(a,b),f=1,g=function(){if(0==--f){var a=e;e=null,c(a)}},h=function(a,b){e&&(e[b]=a,g())},i=function(a){e&&(e=null,d(a))};A(b?e:a,function(a,b){++f,C(a)?a.then(function(a){return h(a,b)},i):h(a,b)}),g()})};c.nodeify=h;var I=c.Cancel=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"this action has been canceled";f(this,a),Object.defineProperty(this,"message",{enumerable:!0,value:b})}return l(a,[{key:"toString",value:function(){return`Cancel: ${this._message}`}}]),a}(),J="CancelToken",K="function"==typeof Symbol&&Symbol.toStringTag||"@@toStringTag",L=c.CancelToken=function(){function a(b){var c=this;f(this,a),this._listeners=null,this._promise=null,this._reason=void 0,this._resolve=null;var d=function(a){d=u;var b=c._reason=a instanceof I?a:new I(a),e=c._resolve;null!==e&&(c._resolve=null,e(b));var f=c._listeners;null!==f&&(c._listeners=null,v(f,function(a){a(b)}))};b(function(a){return d(a)})}return l(a,null,[{key:"isCancelToken",value:function(a){return null!=a&&"function"==typeof a[K]&&a[K]()===J}},{key:"source",value:function(){var b,c=new a(function(a){b=a});return{cancel:b,token:c}}},{key:"race",value:function(b,c){return new a(function(a){A(b,function(b){var c=b.reason;return void 0===c?void(b._listeners||(b._listeners=[])).push(a):(a(c),!1)}),void 0!==c&&c(a)})}}]),l(a,[{key:"fork",value:function(b){if(void 0!==b)return a.race([this],b);var d,c=a.race([this],function(a){d=a});return{cancel:d,token:c}}},{key:"throwIfRequested",value:function(){var a=this._reason;if(void 0!==a)throw a}},{key:K,value:function(){return J}},{key:"promise",get:function(){var a=this,b=this._promise;if(null===b){var c=this._reason;b=this._promise=void 0===c?new Promise(function(b){a._resolve=b}):Promise.resolve(c)}return b}},{key:"reason",get:function(){return this._reason}},{key:"requested",get:function(){return void 0!==this._reason}}]),a}(),M=c.cancelable=function(a,b,c){function d(){var a=arguments.length;if(0!==a&&L.isCancelToken(arguments[0]))return e.apply(this,arguments);var b=L.source(),c=b.cancel,d=b.token,f=Array(a+1);f[0]=d;for(var g=0;g<a;++g)f[g+1]=arguments[g];var h=e.apply(this,f);return h.cancel=c,h}var e=c===void 0?a:c.value;return void 0===c?d:(c.value=d,c)};c.cancellable=M;var N=function(a){return!(a instanceof ReferenceError||a instanceof SyntaxError||a instanceof TypeError)},O={error:null},P=function(a){try{return a()}catch(a){return O.error=a,O}},Q=function(a,b){return"function"==typeof a?a.prototype instanceof Error?b instanceof a:a(b):"object"==typeof a?null!=b&&P(function(){return z(a,function(a,c){if(b[c]!==a)throw null})})!==O:void 0},R=c.defer=function(){var a,b,c=new Promise(function(c,d){a=c,b=d});return{promise:c,reject:b,resolve:a}},S=c.makeAsyncIterator=function(a){var b=D(a);return function(a){return E(this).then(function(c){return b(c,a)}).then(u)}},T=c.forArray=S(v),U=c.forEach=S(A),V=c.forIn=S(w),W=c.forIterable=S(x),X=c.forOwn=S(z),Y=c.fromCallback=function(a){return new Promise(function(b,c){a(function(a,d){return a?c(a):b(d)})})};c.finally=i;var Z=function(){var a=Object.defineProperties;try{var b=a(function(){},{length:{value:2},name:{value:"foo"}});if(2===b.length&&"foo"===b.name)return function(b,c,d){return a(b,{length:{configurable:!0,value:d},name:{configurable:!0,value:c}})}}catch(a){}return function(a){return a}}(),$=c.promisify=function(a,b){return Z(function(){for(var c=this,d=arguments.length,e=Array(d+1),f=0;f<d;++f)e[f]=arguments[f];return new Promise(function(f,g){e[d]=function(a,b){return a?g(a):f(b)},a.apply(b||c,e)})},a.name,a.length&&a.length-1)},_=function(a){return!(n(a,"Sync")||n(a,"Async"))&&a},aa=c.promisifyAll=function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},c=b.mapper,d=void 0===c?_:c,e=b.target,f=void 0===e?{}:e,g=b.context,h=void 0===g?a:g;return w(a,function(b,c){var e;"function"==typeof b&&(e=d(c,b,a))&&(f[e]=$(b,h))}),f},ba=function(){return!1},ca=function(){return!0},da=function(a){return function(b){return{__proto__:a,value:function(){return b}}}}({isFulfilled:ca,isPending:ba,isRejected:ba,isResolved:ca,reason:function(){throw new Error("no reason, the promise has resolved")}}),ea=function(a){return function(b){return{__proto__:a,reason:function(){return b}}}}({isFulfilled:ba,isPending:ba,isRejected:ca,isResolved:ba,value:function(){throw new Error("no value, the promise has rejected")}}),fa=function(a,b){return new Promise(function(c,d){var e=[],f=[],g=function(a){e&&(e.push(a),0==--b&&(c(e),e=f=null))},h=-b,i=function(a){e&&(f.push(a),0==--h&&(d(f),e=f=null))};A(a,function(a){++h,E(a).then(g,i)})})},ga=c.TimeoutError=function(a){function b(){return f(this,b),d(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,"operation timed out"))}return e(b,a),b}(m.BaseError),ha=c.attempt=function(a){return new Promise(function(b){return b(a())})};c.try=ha},{"make-error":2}],2:[function(a,b,c){"use strict";function d(a){a&&e(this,"message",{configurable:!0,value:a,writable:!0});var b=this.constructor.name;b&&b!==this.name&&e(this,"name",{configurable:!0,value:b,writable:!0}),f(this,this.constructor)}var e=Object.defineProperty,f=Error.captureStackTrace;f||(f=function(a){var b=new Error;e(a,"stack",{configurable:!0,get:function(){var a=b.stack;return e(this,"stack",{value:a}),a},set:function(b){e(a,"stack",{configurable:!0,value:b,writable:!0})}})}),d.prototype=Object.create(Error.prototype,{constructor:{configurable:!0,value:d,writable:!0}});var g=function(){function a(a,b){return e(a,"name",{configurable:!0,value:b})}try{var b=function(){};if(a(b,"foo"),"foo"===b.name)return a}catch(a){}}();c=b.exports=function(a,b){if(null==b||b===Error)b=d;else if("function"!=typeof b)throw new TypeError("super_ should be a function");var c;if("string"==typeof a)c=a,a=function(){b.apply(this,arguments)},g&&(g(a,c),c=null);else if("function"!=typeof a)throw new TypeError("constructor should be either a string or a function");a.super_=a["super"]=b;var e={constructor:{configurable:!0,value:a,writable:!0}};return null!=c&&(e.name={configurable:!0,value:c,writable:!0}),a.prototype=Object.create(b.prototype,e),a},c.BaseError=d},{}]},{},[1])(1)}); | ||
{ | ||
"name": "promise-toolbox", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"license": "ISC", | ||
@@ -39,10 +39,10 @@ "description": "Essential utils for promises", | ||
"babel-eslint": "^7.2.3", | ||
"babel-preset-env": "^1.4.0", | ||
"babel-preset-env": "^1.5.1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"babili": "^0.0.12", | ||
"browserify": "^14.3.0", | ||
"cross-env": "^4.0.0", | ||
"babili": "^0.1.2", | ||
"browserify": "^14.4.0", | ||
"cross-env": "^5.0.0", | ||
"dependency-check": "^2.8.0", | ||
"husky": "^0.13.3", | ||
"jest": "^19.0.2", | ||
"husky": "^0.13.4", | ||
"jest": "^20.0.4", | ||
"rimraf": "^2.6.1", | ||
@@ -49,0 +49,0 @@ "standard": "^10.0.2" |
@@ -42,3 +42,3 @@ # promise-toolbox [![Build Status](https://travis-ci.org/JsCommunity/promise-toolbox.png?branch=master)](https://travis-ci.org/JsCommunity/promise-toolbox) | ||
### Cancellation | ||
### Cancelation | ||
@@ -49,3 +49,3 @@ This library provides an implementation of `CancelToken` from the | ||
A cancel token is an object which can be passed to asynchronous | ||
functions to represent cancellation state. | ||
functions to represent cancelation state. | ||
@@ -59,6 +59,6 @@ ```js | ||
A cancel token is created by the initiator of the async work and its | ||
cancellation state may be requested at any time. | ||
cancelation state may be requested at any time. | ||
```js | ||
// Create a token which requests cancellation when a button is clicked. | ||
// Create a token which requests cancelation when a button is clicked. | ||
const token = new CancelToken(cancel => { | ||
@@ -77,5 +77,5 @@ $('#some-button').on('click', () => cancel('button clicked')) | ||
1. synchronously check whether cancellation has been requested | ||
2. synchronously throw if cancellation has been requested | ||
3. register a callback that will be executed if cancellation is requested | ||
1. synchronously check whether cancelation has been requested | ||
2. synchronously throw if cancelation has been requested | ||
3. register a callback that will be executed if cancelation is requested | ||
4. pass the token to subtasks | ||
@@ -86,3 +86,3 @@ | ||
if (token.reason) { | ||
console.log('cancellation has been requested', token.reason.message) | ||
console.log('cancelation has been requested', token.reason.message) | ||
} | ||
@@ -94,3 +94,3 @@ | ||
} catch (reason) { | ||
console.log('cancellation has been requested', reason.message) | ||
console.log('cancelation has been requested', reason.message) | ||
} | ||
@@ -100,3 +100,3 @@ | ||
token.promise.then(reason => { | ||
console.log('cancellation has been requested', reason.message) | ||
console.log('cancelation has been requested', reason.message) | ||
}) | ||
@@ -118,7 +118,7 @@ | ||
> Create a token which is cancelled as soon as one token amongst many | ||
> Create a token which is canceled as soon as one token amongst many | ||
> is. | ||
```js | ||
// `token` will be cancelled (synchronously) as soon as `token1` or | ||
// `token` will be canceled (synchronously) as soon as `token1` or | ||
// `token2` or token3` is, with the same reason. | ||
@@ -130,7 +130,7 @@ const token = CancelToken.race([ token1, token2, token3 ]) | ||
> Create a new token which is cancelled as soon as the original token | ||
> Create a new token which is canceled as soon as the original token | ||
> is or as soon as the executor decides. | ||
```js | ||
// token is cancelled as soon as otherToken is or when the button is | ||
// token is canceled as soon as otherToken is or when the button is | ||
// clicked. | ||
@@ -149,7 +149,7 @@ const token = otherToken.fork(cancel => { | ||
#### @cancellable decorator | ||
#### @cancelable decorator | ||
> Make your async functions cancellable. | ||
> Make your async functions cancelable. | ||
If the first argument passed to the cancellable function is not a | ||
If the first argument passed to the cancelable function is not a | ||
cancel token, a new one is created and injected and the returned | ||
@@ -159,7 +159,7 @@ promise will have a `cancel()` method. | ||
```js | ||
import { cancellable, CancelToken } from 'promise-toolbox' | ||
import { cancelable, CancelToken } from 'promise-toolbox' | ||
const asyncFunction = cancellable(async ($cancelToken, a, b) => { | ||
const asyncFunction = cancelable(async ($cancelToken, a, b) => { | ||
$cancelToken.promise.then(() => { | ||
// do stuff regarding the cancellation request. | ||
// do stuff regarding the cancelation request. | ||
}) | ||
@@ -181,7 +181,7 @@ | ||
If the function is a method of a class or an object, you can use | ||
`cancellable` as a decorator: | ||
`cancelable` as a decorator: | ||
```js | ||
class MyClass { | ||
@cancellable | ||
@cancelable | ||
async asyncMethod ($cancelToken, a, b) { | ||
@@ -309,2 +309,19 @@ // ... | ||
#### wrapApply(fn, args, [thisArg]) / wrapCall(fn, arg, [thisArg]) | ||
> Wrap a call to a function to always return a promise. | ||
```js | ||
function getUserById (id) { | ||
if (typeof id !== 'number') { | ||
throw new TypeError('id must be a number') | ||
} | ||
return db.getUser(id) | ||
} | ||
wrapCall(getUserById, 'foo').catch(error => { | ||
// id must be a number | ||
}) | ||
``` | ||
### Pseudo-methods | ||
@@ -311,0 +328,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
585
103211
931