Comparing version 0.5.0 to 0.5.1
{ | ||
"name": "q", | ||
"description": "A library for promises (CommonJS/Promises/A,B,D)", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"homepage": "http://github.com/kriskowal/q/", | ||
@@ -6,0 +6,0 @@ "author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)", |
185
q.js
@@ -48,5 +48,16 @@ // vim:ts=4:sts=4:sw=4: | ||
// browsers | ||
enqueue = function (task) { | ||
setTimeout(task, 0); | ||
}; | ||
if (typeof MessageChannel !== "undefined") { | ||
// modern browsers | ||
// http://www.nonblocking.io/2011/06/windownexttick.html | ||
var channel = new MessageChannel(); | ||
enqueue = function (task) { | ||
channel.port1.onmessage = task; | ||
channel.port2.postMessage(); | ||
}; | ||
} else { | ||
// old browsers | ||
enqueue = function (task) { | ||
setTimeout(task, 0); | ||
}; | ||
} | ||
} | ||
@@ -64,2 +75,8 @@ | ||
} | ||
var keys = Object.keys || function (object) { | ||
var keys = []; | ||
for (var key in object) | ||
keys.push(key); | ||
return keys; | ||
}; | ||
var reduce = Array.prototype.reduce || function (callback, basis) { | ||
@@ -184,43 +201,24 @@ for (var i = 0, ii = this.length; i < ii; i++) { | ||
Promise.prototype.wait = function () { | ||
return reduce.call(arguments, function (self, next) { | ||
return when(next, function () { | ||
return self; | ||
}); | ||
}, this); | ||
}; | ||
// Chainable methods | ||
reduce.call( | ||
[ | ||
"get", "put", "del", | ||
"post", "invoke", | ||
"keys", | ||
"apply", "call", | ||
"wait", "join", | ||
"fail", "fin", "spy", | ||
"report", "end" | ||
], | ||
function (prev, name) { | ||
Promise.prototype[name] = function () { | ||
return exports[name].apply( | ||
exports, | ||
[this].concat(Array.prototype.slice.call(arguments)) | ||
); | ||
}; | ||
}, | ||
undefined | ||
) | ||
Promise.prototype.join = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(this); | ||
var callback = args.pop(); | ||
return reduce.call(args, function (done, next, i) { | ||
return when(next, function (next) { | ||
return when(done, function () { | ||
args[i] = next; | ||
}); | ||
}); | ||
}, undefined) | ||
.then(function () { | ||
return callback.apply(undefined, args); | ||
}); | ||
}; | ||
Promise.prototype.end = function () { | ||
end(this); | ||
}; | ||
Promise.prototype.report = function (error) { | ||
if (!error) { | ||
error = new Error("REPORT"); | ||
} else if (!error.message) { | ||
error = new Error(error || "REPORT"); | ||
} | ||
return when(this, undefined, function (reason) { | ||
print(error && error.stack || error); | ||
print(reason && reason.stack || reason); | ||
return reject(reason); | ||
}); | ||
}; | ||
Promise.prototype.toSource = function () { | ||
@@ -352,4 +350,9 @@ return this.toString(); | ||
}, | ||
"apply": function (self, args) { | ||
if (!object || typeof object.apply !== "function") | ||
return reject("" + object + " is not a function"); | ||
return object.apply(self, args); | ||
}, | ||
"keys": function () { | ||
return Object.keys(object); | ||
return keys(object); | ||
} | ||
@@ -370,2 +373,3 @@ }, undefined, function valueOf() { | ||
*/ | ||
exports.master = | ||
exports.def = def; | ||
@@ -562,2 +566,21 @@ function def(object) { | ||
/** | ||
* Applies the promised function in a future turn. | ||
* @param object promise or immediate reference for target function | ||
* @param context the context object (this) for the call | ||
* @param args array of application arguments | ||
*/ | ||
var apply = exports.apply = Method("apply"); | ||
/** | ||
* Calls the promised function in a future turn. | ||
* @param object promise or immediate reference for target function | ||
* @param context the context object (this) for the call | ||
* @param ...args array of application arguments | ||
*/ | ||
exports.call = function (value, context) { | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
return apply(value, context, args); | ||
}; | ||
/** | ||
* Requests the names of the owned properties of a promised | ||
@@ -571,2 +594,76 @@ * object in a future turn. | ||
/** | ||
*/ | ||
exports.wait = function (promise) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return reduce.call(args, function (promise, next) { | ||
return when(next, function () { | ||
return promise; | ||
}); | ||
}, promise); | ||
}; | ||
/** | ||
*/ | ||
exports.join = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
var callback = args.pop(); | ||
return reduce.call(args, function (done, next, i) { | ||
return when(next, function (next) { | ||
return when(done, function () { | ||
args[i] = next; | ||
}); | ||
}); | ||
}, undefined) | ||
.then(function () { | ||
return callback.apply(undefined, args); | ||
}); | ||
}; | ||
/** | ||
*/ | ||
exports.fail = fail; | ||
function fail(promise, rejected) { | ||
return when(promise, undefined, rejected); | ||
} | ||
/** | ||
*/ | ||
exports.fin = fin; | ||
function fin(promise, callback) { | ||
return when(promise, function (value) { | ||
return callback(value, undefined); | ||
}, function (reason) { | ||
return callback(undefined, reason); | ||
}); | ||
} | ||
/** | ||
*/ | ||
exports.spy = spy; | ||
function spy(promise, callback) { | ||
return when(promise, function (value) { | ||
callback(value, undefined); | ||
return value; | ||
}, function (reason) { | ||
callback(undefined, reason); | ||
return reject(reason); | ||
}); | ||
} | ||
/** | ||
*/ | ||
exports.report = report; | ||
function report(promise, message) { | ||
var error = new Error(message || "REPORT"); | ||
return spy(promise, function (value, reason) { | ||
print(error && error.stack || error); | ||
if (reason) { | ||
print(reason && reason.stack || reason); | ||
} else { | ||
print(value); | ||
} | ||
}); | ||
} | ||
/** | ||
* Terminates a chain of promises, forcing rejections to be | ||
@@ -573,0 +670,0 @@ * thrown as exceptions. |
11
q.min.js
@@ -1,1 +0,10 @@ | ||
(function(a,b){typeof define=="function"?define(function(b,c){a(b,c)}):typeof exports=="object"?a(require,exports):a(b,Q={})})(function(a,b,c){function y(a){var b=Array.prototype.slice.call(arguments,1);d(function(){a.promiseSend.apply(a,b)})}function x(a){throw a}function v(a,b){var d=j(),e=Array.prototype.slice.call(arguments,2);y.apply(c,[q(a),b,d.resolve].concat(e));return d.promise}function u(a){return function(b){var d=Array.prototype.slice.call(arguments,1);return v.apply(c,[b,a].concat(d))}}function s(a,b,c){function g(a){try{return c?c(a):p(a)}catch(b){i(b&&b.stack||b);return p(b)}}function f(a){try{return b?b(a):a}catch(c){typeof process!="undefined"?process.emit("uncaughtException",c):i(c&&c.stack||c);return p(c)}}var d=j(),e=!1;y(q(a),"when",function(a){e||(e=!0,d.resolve(q(a).promiseSend("when",f,g)))},function(a){e||(e=!0,d.resolve(g(a)))});return d.promise}function r(a){return k({isDef:function(){}},function(b){var d=Array.prototype.slice.call(arguments);return v.apply(c,[a].concat(d))},function(){return a.valueOf()})}function q(a){if(l(a))return a;if(a&&typeof a.then=="function")return k({},function(b,c){if(b!=="when")return Q.when(a,function(a){return Q.ref(a).promiseSend.apply(null,arguments)});var d=j();a.then(d.resolve,d.reject);return d.promise});return k({when:function(b){return a},get:function(b){if(a===c||a===null)return p("Cannot access property "+b+" of "+a);return a[b]},put:function(b,d){if(a===c||a===null)return p("Cannot set property "+b+" of "+a+" to "+d);return a[b]=d},del:function(b){if(a===c||a===null)return p("Cannot delete property "+b+" of "+a);return delete a[b]},post:function(b,d){if(a===c||a===null)return p(""+a+" has no methods");var e=a[b];if(!e)return p("No such method "+b+" on object "+a);if(!e.apply)return p("Property "+b+" on object "+a+" is not a method");return a[b].apply(a,d)},keys:function(){return Object.keys(a)}},c,function(){return a})}function p(a){return k({when:function(b){return b?b(a):p(a)}},function(b){return p(a)},function(){var b=h(p.prototype);b.promiseRejected=!0,b.reason=a;return b})}function o(a){a=t(a);if(a===c||a===null)return!1;return!!a.promiseRejected}function n(a){return!l(t(a))&&!o(a)}function m(a){return!l(t(a.valueOf()))}function l(a){return a&&typeof a.promiseSend=="function"}function k(a,b,d){b===c&&(b=function(a){return p("Promise does not support operation: "+a)});var e=h(k.prototype);e.promiseSend=function(c,d){var e=Array.prototype.slice.call(arguments,2),g;a[c]?g=a[c].apply(a,e):g=b.apply(a,[c].concat(e)),d=d||f;return d(g)},d&&(e.valueOf=d);return g(e)}function j(){var a=[],b,d=h(k.prototype);d.promiseSend=function(){var d=Array.prototype.slice.call(arguments);a?a.push(d):y.apply(c,[b].concat(d))},d.valueOf=function(){if(a)return d;return t(b)};var e=function(d){var e,f,g;if(!!a){b=q(d);for(e=0,f=a.length;e<f;++e)y.apply(c,[b].concat(a[e]));a=c;return b}};return{promise:g(d),resolve:e,reject:function(a){return e(p(a))}}}function f(a){return a}"use strict";var d;try{d=a("event-queue").enqueue}catch(e){d=function(a){setTimeout(a,0)}}var g=Object.freeze||f,h=Object.create||function h(a){var b=function(){};b.prototype=a;return new b},i=typeof console=="undefined"?f:function(a){console.log(a)};b.enqueue=d,b.defer=j,b.makePromise=k,k.prototype.then=function(a,b){return s(this,a,b)},k.prototype.end=function(){return s(this,c,x)},k.prototype.toSource=function(){return this.toString()},k.prototype.toString=function(){return"[object Promise]"},g(k.prototype),b.isPromise=l,b.isResolved=m,b.isFulfilled=n,b.isRejected=o,b.reject=p,p.prototype=h(k.prototype,{constructor:{value:p}}),b.ref=q,b.def=r,b.when=s,b.asap=function(a,b,c){b=b||f;if(n(a))return t(b(t(a)));if(o(a)){var d=a.valueOf().reason;if(c)return c(d);throw d}return s(a,b,c)};var t=function(a){return a===c||a===null?a:a.valueOf()};b.Method=u,b.send=v,b.get=u("get"),b.put=u("put"),b.del=u("del");var w=b.post=u("post");b.invoke=function(a,b){var c=Array.prototype.slice.call(arguments,2);return w(a,b,c)},b.keys=u("keys"),b.error=x}) | ||
(function(m,c){typeof define==="function"?define(function(c,j){m(c,j)}):typeof exports==="object"?m(require,exports):m(c,Q={})})(function(m,c,e){function j(a){return a}function o(){var a=[],b,d=p(g.prototype);d.promiseSend=function(){var d=Array.prototype.slice.call(arguments);a?a.push(d):q.apply(e,[b].concat(d))};d.valueOf=function(){if(a)return d;return k(b)};var c=function(d){var c;if(a){b=l(d);d=0;for(c=a.length;d<c;++d)q.apply(e,[b].concat(a[d]));a=e;return b}};return{promise:s(d),resolve:c, | ||
reject:function(a){return c(f(a))}}}function g(a,b,d){b===e&&(b=function(a){return f("Promise does not support operation: "+a)});var c=p(g.prototype);c.promiseSend=function(d,c){var e=Array.prototype.slice.call(arguments,2),e=a[d]?a[d].apply(a,e):b.apply(a,[d].concat(e)),c=c||j;return c(e)};if(d)c.valueOf=d;return s(c)}function r(a){return a&&typeof a.promiseSend==="function"}function w(a){return!r(k(a))&&!t(a)}function t(a){a=k(a);if(a===e||a===null)return!1;return!!a.promiseRejected}function f(a){return g({when:function(b){return b? | ||
b(a):f(a)}},function(){return f(a)},function(){var b=p(f.prototype);b.promiseRejected=!0;b.reason=a;return b})}function l(a){if(r(a))return a;if(a&&typeof a.then==="function")return g({},function(b){return b!=="when"?h(a,function(a){return l(a).promiseSend.apply(null,arguments)}):(b=o(),a.then(b.resolve,b.reject),b.promise)});return g({when:function(){return a},get:function(b){if(a===e||a===null)return f("Cannot access property "+b+" of "+a);return a[b]},put:function(b,d){if(a===e||a===null)return f("Cannot set property "+ | ||
b+" of "+a+" to "+d);return a[b]=d},del:function(b){if(a===e||a===null)return f("Cannot delete property "+b+" of "+a);return delete a[b]},post:function(b,d){if(a===e||a===null)return f(""+a+" has no methods");var c=a[b];if(!c)return f("No such method "+b+" on object "+a);if(!c.apply)return f("Property "+b+" on object "+a+" is not a method");return a[b].apply(a,d)},apply:function(b,d){if(!a||typeof a.apply!=="function")return f(""+a+" is not a function");return a.apply(b,d)},keys:function(){return y(a)}}, | ||
e,function(){return a})}function h(a,b,d){function c(a){try{return b?b(a):a}catch(d){return f(d)}}function e(a){try{return d?d(a):f(a)}catch(b){return f(b)}}var g=o(),h=!1;q(l(a),"when",function(a){h||(h=!0,g.resolve(l(a).promiseSend("when",c,e)))},function(a){h||(h=!0,g.resolve(e(a)))});return g.promise}function i(a){return function(b){var d=Array.prototype.slice.call(arguments,1);return u.apply(e,[b,a].concat(d))}}function u(a,b){var d=o(),c=Array.prototype.slice.call(arguments,2);q.apply(e,[l(a), | ||
b,d.resolve].concat(c));return d.promise}function q(a){var b=Array.prototype.slice.call(arguments,1);n(function(){a.promiseSend.apply(a,b)})}var n;try{n=m("event-queue").enqueue}catch(B){n=function(a){setTimeout(a,0)}}var s=Object.freeze||j,p=Object.create||function(a){var b=function(){};b.prototype=a;return new b},y=Object.keys||function(a){var b=[],d;for(d in a)b.push(d);return b},v=Array.prototype.reduce||function(a,b){for(var d=0,c=this.length;d<c;d++)b=a(b,this[d],d);return b},x=typeof console=== | ||
"undefined"?j:function(a){console.log(a)};c.enqueue=n;c.defer=o;c.makePromise=g;g.prototype.then=function(a,b){return h(this,a,b)};v.call(["get","put","del","post","invoke","keys","apply","call","wait","join","report","end"],function(a,b){g.prototype[b]=function(){return c[b].apply(c,[this].concat(Array.prototype.slice.call(arguments)))}},e);g.prototype.toSource=function(){return this.toString()};g.prototype.toString=function(){return"[object Promise]"};s(g.prototype);c.isPromise=r;c.isResolved=function(a){return!r(k(a.valueOf()))}; | ||
c.isFulfilled=w;c.isRejected=t;c.reject=f;f.prototype=p(g.prototype,{constructor:{value:f}});c.ref=l;c.def=function(a){return g({isDef:function(){}},function(){var b=Array.prototype.slice.call(arguments);return u.apply(e,[a].concat(b))},function(){return a.valueOf()})};c.when=h;c.asap=function(a,b,d){b=b||j;if(w(a))return k(b(k(a)));else if(t(a))if(a=a.valueOf().reason,d)return d(a);else throw a;else return h(a,b,d)};var k=function(a){return a===e||a===null?a:a.valueOf()};c.Method=i;c.send=u;c.get= | ||
i("get");c.put=i("put");c.del=i("del");var z=c.post=i("post");c.invoke=function(a,b){var d=Array.prototype.slice.call(arguments,2);return z(a,b,d)};var A=c.apply=i("apply");c.call=function(a,b){var d=Array.prototype.slice.call(arguments,2);return A(a,b,d)};c.keys=i("keys");c.wait=function(a){var b=Array.prototype.slice.call(arguments,1);return v.call(b,function(a,b){return h(b,function(){return a})},a)};c.join=function(){var a=Array.prototype.slice.call(arguments),b=a.pop();return v.call(a,function(b, | ||
c,e){return h(c,function(c){return h(b,function(){a[e]=c})})},e).then(function(){return b.apply(e,a)})};c.report=function(a,b){b?b.message||(b=Error(b||"REPORT")):b=Error("REPORT");return h(a,e,function(a){x(b&&b.stack||b);x(a&&a.stack||a);return f(a)})};c.end=function(a){h(a,e,function(a){n(function(){throw a;})})}}); |
@@ -5,3 +5,2 @@ | ||
Q.when(1) | ||
.wait(Q.reject(new Error())) | ||
.wait(3, 4, 5) | ||
@@ -8,0 +7,0 @@ .join(2, 3, function (one, two, three) { |
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
129709
2674
88