promise-toolbox
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -65,3 +65,3 @@ 'use strict';Object.defineProperty(exports,"__esModule",{value:true});exports.TimeoutError=exports.finally=exports.fromCallback=exports.forOwn=exports.forIterable=exports.forIn=exports.forEach=exports.forArray=exports.makeAsyncIterator=exports.defer=exports.cancellable=exports.nodeify=exports.isPromise=undefined;var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj;};exports.all=all;exports.asCallback=asCallback;exports.catchPlus=catchPlus;exports.delay=delay;exports.join=join;exports.lastly=lastly;exports.promisify=promisify;exports.promisifyAll=promisifyAll;exports.reflect=reflect;exports.settle=settle;exports.some=some;exports.timeout=timeout;var _makeError=require('make-error');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;} // =================================================================== | ||
function promisify(context){var fn=this;return _setFunctionNameAndLength(function(){var _this2=this;var _arguments=arguments;var length=_arguments.length;var args=new Array(length+1);for(var i=0;i<length;++i){args[i]=arguments[i];}return new Promise(function(resolve,reject){args[length]=function(error,result){return error?reject(error):resolve(result);};fn.apply(context||_this2,args);});},fn.name,fn.length&&fn.length-1);}var _DEFAULT_PALL_MAPPER=function _DEFAULT_PALL_MAPPER(name,fn){return !(_endsWith(name,'Sync')||_endsWith(name,'Async'))&&name;}; // Usage: obj::promisifyAll([ opts ]) | ||
function promisifyAll(){var _this3=this;var _ref=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];var _ref$mapper=_ref.mapper;var mapper=_ref$mapper===undefined?_DEFAULT_PALL_MAPPER:_ref$mapper;var _ref$target=_ref.target;var target=_ref$target===undefined?{}:_ref$target;var _ref$context=_ref.context;var context=_ref$context===undefined?target:_ref$context;_forIn(this,function(value,name){var newName=undefined;if(typeof value==='function'&&(newName=mapper(name,value,_this3))){target[newName]=promisify.call(value,context);}});return target;} // ------------------------------------------------------------------- | ||
function promisifyAll(){var _this3=this;var _ref=arguments.length<=0||arguments[0]===undefined?{}:arguments[0];var _ref$mapper=_ref.mapper;var mapper=_ref$mapper===undefined?_DEFAULT_PALL_MAPPER:_ref$mapper;var _ref$target=_ref.target;var target=_ref$target===undefined?{}:_ref$target;var _ref$context=_ref.context;var context=_ref$context===undefined?this:_ref$context;_forIn(this,function(value,name){var newName=undefined;if(typeof value==='function'&&(newName=mapper(name,value,_this3))){target[newName]=promisify.call(value,context);}});return target;} // ------------------------------------------------------------------- | ||
var FN_FALSE=function FN_FALSE(){return false;};var FN_TRUE=function FN_TRUE(){return true;};var _reflectResolution=function(__proto__){return function(_value){return {__proto__:__proto__,value:function value(){return _value;}};};}({isFulfilled:FN_TRUE,isPending:FN_FALSE,isRejected:FN_FALSE,isResolved:FN_TRUE,reason:function reason(){throw new Error('no reason, the promise has resolved');}});var _reflectRejection=function(__proto__){return function(_reason){return {__proto__:__proto__,reason:function reason(){return _reason;}};};}({isFulfilled:FN_FALSE,isPending:FN_FALSE,isRejected:FN_TRUE,isResolved:FN_FALSE,value:function value(){throw new Error('no value, the promise has rejected');}}); // Returns a promise that is always successful when this promise is | ||
@@ -68,0 +68,0 @@ // settled. Its fulfillment value is an object that implements the |
@@ -10,2 +10,3 @@ 'use strict';var _slicedToArray=function(){function sliceIterator(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally {try{if(!_n&&_i["return"])_i["return"]();}finally {if(_d)throw _e;}}return _arr;}return function(arr,i){if(Array.isArray(arr)){return arr;}else if(Symbol.iterator in Object(arr)){return sliceIterator(arr,i);}else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}(); /* eslint-env mocha */var _must=require('must');var _must2=_interopRequireDefault(_must);var _makeError=require('make-error');var _makeError2=_interopRequireDefault(_makeError);var _sinon=require('sinon');var _sinon2=_interopRequireDefault(_sinon);var _=require('./');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};} // =================================================================== | ||
describe('lastly()',function(){it('calls a callback on resolution',function(){var _context5;var value={};var spy=_sinon2.default.spy();return (0,_must2.default)((_context5=Promise.resolve(value),_.lastly).call(_context5,spy)).to.resolve.to.equal(value).then(function(){(0,_must2.default)(spy.callCount).to.equal(1);});});it('calls a callback on rejection',function(){var _context6;var reason={};var spy=_sinon2.default.spy();return (0,_must2.default)((_context6=Promise.reject(reason),_.lastly).call(_context6,spy)).to.reject.to.equal(reason).then(function(){(0,_must2.default)(spy.callCount).to.equal(1);});});}); // ------------------------------------------------------------------- | ||
describe('promisifyAll()',function(){it('returns a new object',function(){var o={};var r=_.promisifyAll.call(o);(0,_must2.default)(r).to.be.an.object();(0,_must2.default)(r).to.not.equal(o);});it('creates promisified version of all functions bound to the original object',function(){var o={foo:function foo(cb){cb(null,this);}};var r=_.promisifyAll.call(o);return (0,_must2.default)(r.foo()).to.resolve.to.equal(o);});it('ignores functions ending with Sync or Async',function(){var o={fooAsync:function fooAsync(){},fooSync:function fooSync(){}};var r=_.promisifyAll.call(o);(0,_must2.default)(r).to.not.have.property('foo');(0,_must2.default)(r).to.not.have.property('fooASync');(0,_must2.default)(r).to.not.have.property('fooSync');});}); // ------------------------------------------------------------------- | ||
describe('settle()',function(){it('works with arrays',function(){var _context7;return (_context7=[Promise.resolve(42),Math.PI,Promise.reject('fatality')],_.settle).call(_context7).then(function(_ref){var _ref2=_slicedToArray(_ref,3);var status1=_ref2[0];var status2=_ref2[1];var status3=_ref2[2];(0,_must2.default)(status1.isFulfilled()).to.equal(true);(0,_must2.default)(status2.isFulfilled()).to.equal(true);(0,_must2.default)(status3.isFulfilled()).to.equal(false); // Alias. | ||
@@ -12,0 +13,0 @@ (0,_must2.default)(status1.isResolved()).to.equal(true);(0,_must2.default)(status2.isResolved()).to.equal(true);(0,_must2.default)(status3.isResolved()).to.equal(false);(0,_must2.default)(status1.isRejected()).to.equal(false);(0,_must2.default)(status2.isRejected()).to.equal(false);(0,_must2.default)(status3.isRejected()).to.equal(true);(0,_must2.default)(status1.value()).to.equal(42);(0,_must2.default)(status2.value()).to.equal(Math.PI);(0,_must2.default)(status3.value.bind(status3)).to.throw();(0,_must2.default)(status1.reason.bind(status1)).to.throw();(0,_must2.default)(status2.reason.bind(status2)).to.throw();(0,_must2.default)(status3.reason()).to.equal('fatality');});});it('works with objects',function(){var _context8;return (_context8={a:Promise.resolve(42),b:Math.PI,c:Promise.reject('fatality')},_.settle).call(_context8).then(function(_ref3){var status1=_ref3.a;var status2=_ref3.b;var status3=_ref3.c;(0,_must2.default)(status1.isFulfilled()).to.equal(true);(0,_must2.default)(status2.isFulfilled()).to.equal(true);(0,_must2.default)(status3.isFulfilled()).to.equal(false); // Alias. |
{ | ||
"name": "promise-toolbox", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"license": "ISC", | ||
@@ -5,0 +5,0 @@ "description": "Essential utils for promises", |
Sorry, the diff of this file is not supported yet
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
74871
157