Comparing version 0.8.8 to 0.8.9
{ | ||
"name": "q", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "A library for promises (CommonJS/Promises/A,B,D)", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/kriskowal/q/", |
120
q.js
@@ -425,3 +425,3 @@ // vim:ts=4:sts=4:sw=4: | ||
function deprecate(fn, name, alternative) { | ||
function deprecate(callback, name, alternative) { | ||
return function () { | ||
@@ -431,3 +431,3 @@ if (typeof console !== "undefined" && typeof console.warn === "function") { | ||
} | ||
return fn.apply(fn, arguments); | ||
return callback.apply(callback, arguments); | ||
}; | ||
@@ -462,3 +462,3 @@ } | ||
// resolved values and other promises gracefully. | ||
var pending = [], value; | ||
var pending = [], progressListeners = [], value; | ||
@@ -468,6 +468,9 @@ var deferred = object_create(defer.prototype); | ||
promise.promiseSend = function () { | ||
promise.promiseSend = function (op, _, __, progress) { | ||
var args = array_slice(arguments); | ||
if (pending) { | ||
pending.push(args); | ||
if (op === "when" && progress) { | ||
progressListeners.push(progress); | ||
} | ||
} else { | ||
@@ -502,2 +505,3 @@ nextTick(function () { | ||
pending = void 0; | ||
progressListeners = void 0; | ||
return value; | ||
@@ -513,3 +517,14 @@ } | ||
}; | ||
deferred.notify = function () { | ||
if (pending) { | ||
var args = arguments; | ||
array_reduce(progressListeners, function (undefined, progressListener) { | ||
nextTick(function () { | ||
progressListener.apply(void 0, args); | ||
}); | ||
}, void 0); | ||
} | ||
}; | ||
return deferred; | ||
@@ -540,3 +555,3 @@ } | ||
* @param makePromise {Function} a function that returns nothing and accepts | ||
* the resolve and reject functions for a deferred. | ||
* the resolve, reject, and notify functions for a deferred. | ||
* @returns a promise that may be resolved with the given resolve and reject | ||
@@ -551,3 +566,4 @@ * functions, or rejected by a thrown exception in makePromise | ||
deferred.resolve, | ||
deferred.reject | ||
deferred.reject, | ||
deferred.notify | ||
).fail(deferred.reject); | ||
@@ -590,3 +606,5 @@ return deferred.promise; | ||
} | ||
resolved(result); | ||
if (resolved) { | ||
resolved(result); | ||
} | ||
}; | ||
@@ -608,4 +626,4 @@ | ||
// provide thenables, CommonJS/Promises/A | ||
makePromise.prototype.then = function (fulfilled, rejected) { | ||
return when(this, fulfilled, rejected); | ||
makePromise.prototype.then = function (fulfilled, rejected, progressed) { | ||
return when(this, fulfilled, rejected, progressed); | ||
}; | ||
@@ -626,3 +644,6 @@ | ||
"timeout", "delay", | ||
"catch", "finally", "fail", "fin", "end" | ||
"catch", "finally", "fail", "fin", "progress", "end", | ||
"ncall", "napply", "nbind", | ||
"npost", "ninvoke", | ||
"nend" | ||
], | ||
@@ -771,7 +792,16 @@ function (undefined, name) { | ||
} | ||
// In order to break infinite recursion or loops between `then` and | ||
// `resolve`, it is necessary to attempt to extract fulfilled values | ||
// out of foreign promise implementations before attempting to wrap | ||
// them as unresolved promises. It is my hope that other | ||
// implementations will implement `valueOf` to synchronously extract | ||
// the fulfillment value from their fulfilled promises. If the | ||
// other promise library does not implement `valueOf`, the | ||
// implementations on primordial prototypes are harmless. | ||
object = valueOf(object); | ||
// assimilate thenables, CommonJS/Promises/A | ||
if (object && typeof object.then === "function") { | ||
var result = defer(); | ||
object.then(result.resolve, result.reject); | ||
return result.promise; | ||
var deferred = defer(); | ||
object.then(deferred.resolve, deferred.reject, deferred.notify); | ||
return deferred.promise; | ||
} | ||
@@ -786,6 +816,8 @@ return makePromise({ | ||
"put": function (name, value) { | ||
return object[name] = value; | ||
object[name] = value; | ||
return object; | ||
}, | ||
"del": function (name) { | ||
return delete object[name]; | ||
delete object[name]; | ||
return object; | ||
}, | ||
@@ -901,9 +933,10 @@ "post": function (name, value) { | ||
* | ||
* @param value promise or immediate reference to observe | ||
* @param fulfilled function to be called with the fulfilled value | ||
* @param rejected function to be called with the rejection exception | ||
* @param value promise or immediate reference to observe | ||
* @param fulfilled function to be called with the fulfilled value | ||
* @param rejected function to be called with the rejection exception | ||
* @param progressed function to be called on any progress notifications | ||
* @return promise for the return value from the invoked callback | ||
*/ | ||
exports.when = when; | ||
function when(value, fulfilled, rejected) { | ||
function when(value, fulfilled, rejected, progressed) { | ||
var deferred = defer(); | ||
@@ -929,4 +962,5 @@ var done = false; // ensure the untrusted promise makes at most a | ||
var resolvedValue = resolve(value); | ||
nextTick(function () { | ||
resolve(value).promiseSend("when", function (value) { | ||
resolvedValue.promiseSend("when", function (value) { | ||
if (done) { | ||
@@ -948,2 +982,7 @@ return; | ||
// Progress listeners need to be attached in the current tick. | ||
if (progressed) { | ||
resolvedValue.promiseSend("when", void 0, void 0, progressed); | ||
} | ||
return deferred.promise; | ||
@@ -1059,10 +1098,10 @@ } | ||
* | ||
* @param {function} fn The function to decorate | ||
* @param {function} callback The function to decorate | ||
* @returns {function} a function that has been decorated. | ||
*/ | ||
exports.promised = promised; | ||
function promised(fn) { | ||
function promised(callback) { | ||
return function () { | ||
return all([this, all(arguments)]).spread(function (self, args) { | ||
return fn.apply(self, args); | ||
return callback.apply(self, args); | ||
}); | ||
@@ -1346,2 +1385,16 @@ }; | ||
/** | ||
* Attaches a listener that can respond to progress notifications from a | ||
* promise's originating deferred. This listener receives the exact arguments | ||
* passed to ``deferred.notify``. | ||
* @param {Any*} promise for something | ||
* @param {Function} callback to receive any progress notifications | ||
* @returns the given promise, unchanged | ||
*/ | ||
exports.progress = progress; | ||
function progress(promise, progressed) { | ||
when(promise, void 0, void 0, progressed); | ||
return promise; | ||
} | ||
/** | ||
* Provides an opportunity to observe the rejection of a promise, | ||
@@ -1543,2 +1596,23 @@ * regardless of whether the promise is fulfilled or rejected. Forwards | ||
exports.nend = nend; | ||
function nend(promise, nodeback) { | ||
if (nodeback) { | ||
var deferred = defer(); | ||
promise.then(function (value) { | ||
nextTick(function () { | ||
deferred.resolve(); | ||
nodeback(null, value); | ||
}); | ||
}, function (error) { | ||
nextTick(function () { | ||
deferred.resolve(); | ||
nodeback(error); | ||
}); | ||
}); | ||
return deferred.promise; | ||
} else { | ||
return promise; | ||
} | ||
} | ||
defend(exports); | ||
@@ -1545,0 +1619,0 @@ |
@@ -244,4 +244,3 @@ [![Build Status](https://secure.travis-ci.org/kriskowal/q.png)](http://travis-ci.org/kriskowal/q) | ||
function eventualAdd(a, b) { | ||
return Q.all([a, b]) | ||
.spread(function (a, b) { | ||
return Q.spread([a, b], function (a, b) { | ||
return a + b; | ||
@@ -265,12 +264,2 @@ }) | ||
And you can use ``Q.spread`` directly on an array of promises. | ||
```javascript | ||
function eventualAdd(a, b) { | ||
return Q.spread([a, b], function (a, b) { | ||
return a + b; | ||
}) | ||
} | ||
``` | ||
The ``all`` function returns a promise for an array of values. If one | ||
@@ -277,0 +266,0 @@ of the given promise fails, the whole returned promise fails, not |
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
69648
1476
634