Comparing version 0.9.2 to 0.9.3
{ | ||
"name": "q", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "A library for promises (CommonJS/Promises/A,B,D)", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/kriskowal/q", |
192
q.js
@@ -31,3 +31,3 @@ // vim:ts=4:sts=4:sw=4: | ||
// Turn off strict mode for this function so we can assign to global.Q | ||
/*jshint strict: false*/ | ||
/*jshint strict: false, -W117*/ | ||
@@ -77,10 +77,7 @@ // This file will function properly as a <script> tag, or a module | ||
// use the fastest possible means to execute a task in a future turn | ||
// Use the fastest possible means to execute a task in a future turn | ||
// of the event loop. | ||
var nextTick; | ||
if (typeof process !== "undefined") { | ||
// node | ||
nextTick = process.nextTick; | ||
} else if (typeof setImmediate === "function") { | ||
// In IE10, or use https://github.com/NobleJS/setImmediate | ||
if (typeof setImmediate === "function") { | ||
// In IE10, Node.js 0.9+, or https://github.com/NobleJS/setImmediate | ||
if (typeof window !== "undefined") { | ||
@@ -91,8 +88,17 @@ nextTick = setImmediate.bind(window); | ||
} | ||
} else if (typeof process !== "undefined" && process.nextTick) { | ||
// Node.js before 0.9. Note that some fake-Node environments, like the | ||
// Mocha test runner, introduce a `process` global without a `nextTick`. | ||
nextTick = process.nextTick; | ||
} else { | ||
(function () { | ||
// linked list of tasks (single, with head node) | ||
var head = {task: void 0, next: null}, tail = head, | ||
maxPendingTicks = 2, pendingTicks = 0, queuedTasks = 0, usedTicks = 0, | ||
requestTick; | ||
var head = {task: void 0, next: null}; | ||
var tail = head; | ||
var maxPendingTicks = 2; | ||
var pendingTicks = 0; | ||
var queuedTasks = 0; | ||
var usedTicks = 0; | ||
var requestTick = void 0; | ||
@@ -108,3 +114,6 @@ function onTick() { | ||
maxPendingTicks *= 4; // fast grow! | ||
var expectedTicks = queuedTasks && Math.min(queuedTasks - 1, maxPendingTicks); | ||
var expectedTicks = queuedTasks && Math.min( | ||
queuedTasks - 1, | ||
maxPendingTicks | ||
); | ||
while (pendingTicks < expectedTicks) { | ||
@@ -129,3 +138,6 @@ ++pendingTicks; | ||
tail = tail.next = {task: task, next: null}; | ||
if (pendingTicks < ++queuedTasks && pendingTicks < maxPendingTicks) { | ||
if ( | ||
pendingTicks < ++queuedTasks && | ||
pendingTicks < maxPendingTicks | ||
) { | ||
++pendingTicks; | ||
@@ -381,3 +393,3 @@ requestTick(); | ||
// resolved values and other promises gracefully. | ||
var pending = [], progressListeners = [], value; | ||
var pending = [], progressListeners = [], resolvedPromise; | ||
@@ -396,3 +408,3 @@ var deferred = object_create(defer.prototype); | ||
nextTick(function () { | ||
value.promiseDispatch.apply(value, args); | ||
resolvedPromise.promiseDispatch.apply(resolvedPromise, args); | ||
}); | ||
@@ -406,5 +418,5 @@ } | ||
} | ||
var nearer = valueOf(value); | ||
var nearer = valueOf(resolvedPromise); | ||
if (isPromise(nearer)) { | ||
value = nearer; // shorten chain | ||
resolvedPromise = nearer; // shorten chain | ||
} | ||
@@ -420,15 +432,20 @@ return nearer; | ||
// it's always just "[object Promise]\n", as per the `toString`. | ||
promise.stack = promise.stack.substring(promise.stack.indexOf("\n") + 1); | ||
promise.stack = promise.stack.substring( | ||
promise.stack.indexOf("\n") + 1 | ||
); | ||
} | ||
function become(resolvedValue) { | ||
if (!pending) { | ||
return; | ||
} | ||
value = resolve(resolvedValue); | ||
// NOTE: we do the checks for `resolvedPromise` in each method, instead of | ||
// consolidating them into `become`, since otherwise we'd create new | ||
// promises with the lines `become(whatever(value))`. See e.g. GH-252. | ||
function become(promise) { | ||
resolvedPromise = promise; | ||
array_reduce(pending, function (undefined, pending) { | ||
nextTick(function () { | ||
value.promiseDispatch.apply(value, pending); | ||
promise.promiseDispatch.apply(promise, pending); | ||
}); | ||
}, void 0); | ||
pending = void 0; | ||
@@ -439,17 +456,34 @@ progressListeners = void 0; | ||
deferred.promise = promise; | ||
deferred.resolve = become; | ||
deferred.resolve = function (value) { | ||
if (resolvedPromise) { | ||
return; | ||
} | ||
become(resolve(value)); | ||
}; | ||
deferred.fulfill = function (value) { | ||
if (resolvedPromise) { | ||
return; | ||
} | ||
become(fulfill(value)); | ||
}; | ||
deferred.reject = function (exception) { | ||
become(reject(exception)); | ||
deferred.reject = function (reason) { | ||
if (resolvedPromise) { | ||
return; | ||
} | ||
become(reject(reason)); | ||
}; | ||
deferred.notify = function (progress) { | ||
if (pending) { | ||
array_reduce(progressListeners, function (undefined, progressListener) { | ||
nextTick(function () { | ||
progressListener(progress); | ||
}); | ||
}, void 0); | ||
if (resolvedPromise) { | ||
return; | ||
} | ||
array_reduce(progressListeners, function (undefined, progressListener) { | ||
nextTick(function () { | ||
progressListener(progress); | ||
}); | ||
}, void 0); | ||
}; | ||
@@ -479,12 +513,16 @@ | ||
/** | ||
* @param makePromise {Function} a function that returns nothing and accepts | ||
* @param resolver {Function} a function that returns nothing and accepts | ||
* the resolve, reject, and notify functions for a deferred. | ||
* @returns a promise that may be resolved with the given resolve and reject | ||
* functions, or rejected by a thrown exception in makePromise | ||
* functions, or rejected by a thrown exception in resolver | ||
*/ | ||
Q.promise = promise; | ||
function promise(makePromise) { | ||
function promise(resolver) { | ||
if (typeof resolver !== "function") { | ||
throw new TypeError("resolver must be a function."); | ||
} | ||
var deferred = defer(); | ||
fcall( | ||
makePromise, | ||
resolver, | ||
deferred.resolve, | ||
@@ -500,3 +538,3 @@ deferred.reject, | ||
* function. The descriptor contains methods like when(rejected), get(name), | ||
* put(name, value), post(name, args), and delete(name), which all | ||
* set(name, value), post(name, args), and delete(name), which all | ||
* return either a value, a promise for a value, or a rejection. The fallback | ||
@@ -513,3 +551,5 @@ * accepts the operation name, a resolver, and any further arguments that would | ||
fallback = function (op) { | ||
return reject(new Error("Promise does not support operation: " + op)); | ||
return reject(new Error( | ||
"Promise does not support operation: " + op | ||
)); | ||
}; | ||
@@ -566,3 +606,3 @@ } | ||
"when", "spread", | ||
"get", "put", "set", "del", "delete", | ||
"get", "set", "del", "delete", | ||
"post", "send", "invoke", | ||
@@ -575,3 +615,2 @@ "keys", | ||
"nfcall", "nfapply", "nfbind", "denodeify", "nbind", | ||
"ncall", "napply", "nbind", | ||
"npost", "nsend", "ninvoke", | ||
@@ -657,8 +696,12 @@ "nodeify" | ||
var rejections = []; | ||
var errors = []; | ||
var errorsDisplayed; | ||
function displayErrors() { | ||
// This promise library consumes exceptions thrown in handlers so they can be | ||
// handled by a subsequent promise. The exceptions get added to this array when | ||
// they are created, and removed when they are handled. Note that in ES6 or | ||
// shimmed environments, this would naturally be a `Set`. | ||
var unhandledReasons = Q.unhandledReasons = []; | ||
var unhandledRejections = []; | ||
var unhandledReasonsDisplayed = false; | ||
function displayUnhandledReasons() { | ||
if ( | ||
!errorsDisplayed && | ||
!unhandledReasonsDisplayed && | ||
typeof window !== "undefined" && | ||
@@ -668,22 +711,20 @@ !window.Touch && | ||
) { | ||
// This promise library consumes exceptions thrown in handlers so | ||
// they can be handled by a subsequent promise. The rejected | ||
// promises get added to this array when they are created, and | ||
// removed when they are handled. | ||
console.log("Should be empty:", errors); | ||
console.warn("[Q] Unhandled rejection reasons (should be empty):", | ||
unhandledReasons); | ||
} | ||
errorsDisplayed = true; | ||
unhandledReasonsDisplayed = true; | ||
} | ||
// Show unhandled rejection if Node exits without handling an outstanding | ||
// rejection. (Note that Browserify presently produces a process global | ||
// without the Emitter on interface) | ||
// Show unhandled rejection reasons if Node exits without handling an | ||
// outstanding rejection. (Note that Browserify presently produces a process | ||
// global without the `EventEmitter` `on` method.) | ||
if (typeof process !== "undefined" && process.on) { | ||
process.on("exit", function () { | ||
for (var i = 0; i < errors.length; i++) { | ||
var error = errors[i]; | ||
if (error && typeof error.stack !== "undefined") { | ||
console.warn("Unhandled rejected promise:", error.stack); | ||
for (var i = 0; i < unhandledReasons.length; i++) { | ||
var reason = unhandledReasons[i]; | ||
if (reason && typeof reason.stack !== "undefined") { | ||
console.warn("Unhandled rejection reason:", reason.stack); | ||
} else { | ||
console.warn("Unhandled rejected promise (no stack):", error); | ||
console.warn("Unhandled rejection reason (no stack):", reason); | ||
} | ||
@@ -696,6 +737,6 @@ } | ||
* Constructs a rejected promise. | ||
* @param exception value describing the failure | ||
* @param reason value describing the failure | ||
*/ | ||
Q.reject = reject; | ||
function reject(exception) { | ||
function reject(reason) { | ||
var rejection = makePromise({ | ||
@@ -705,19 +746,21 @@ "when": function (rejected) { | ||
if (rejected) { | ||
var at = array_indexOf(rejections, this); | ||
var at = array_indexOf(unhandledRejections, this); | ||
if (at !== -1) { | ||
errors.splice(at, 1); | ||
rejections.splice(at, 1); | ||
unhandledRejections.splice(at, 1); | ||
unhandledReasons.splice(at, 1); | ||
} | ||
} | ||
return rejected ? rejected(exception) : this; | ||
return rejected ? rejected(reason) : this; | ||
} | ||
}, function fallback() { | ||
return reject(exception); | ||
return reject(reason); | ||
}, function valueOf() { | ||
return this; | ||
}, exception, true); | ||
// note that the error has not been handled | ||
displayErrors(); | ||
rejections.push(rejection); | ||
errors.push(exception); | ||
}, reason, true); | ||
// Note that the reason has not been handled. | ||
displayUnhandledReasons(); | ||
unhandledRejections.push(rejection); | ||
unhandledReasons.push(reason); | ||
return rejection; | ||
@@ -1059,3 +1102,3 @@ } | ||
* | ||
* "dispatcher" constructs methods like "get(promise, name)" and "put(promise)". | ||
* "dispatcher" constructs methods like "get(promise, name)" and "set(promise)". | ||
*/ | ||
@@ -1316,2 +1359,3 @@ Q.dispatcher = dispatcher; | ||
* @param {Number} milliseconds timeout | ||
* @param {String} custom error message (optional) | ||
* @returns a promise for the resolution of the given promise if it is | ||
@@ -1321,6 +1365,6 @@ * fulfilled before the timeout, otherwise rejected. | ||
Q.timeout = timeout; | ||
function timeout(promise, ms) { | ||
function timeout(promise, ms, msg) { | ||
var deferred = defer(); | ||
var timeoutId = setTimeout(function () { | ||
deferred.reject(new Error("Timed out after " + ms + " ms")); | ||
deferred.reject(new Error(msg || "Timed out after " + ms + " ms")); | ||
}, ms); | ||
@@ -1327,0 +1371,0 @@ |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://secure.travis-ci.org/kriskowal/q.png)](http://travis-ci.org/kriskowal/q) | ||
[![Build Status](https://secure.travis-ci.org/kriskowal/q.png?branch=master)](http://travis-ci.org/kriskowal/q) | ||
@@ -287,7 +287,8 @@ <a href="http://promises-aplus.github.com/promises-spec"> | ||
The ``all`` function returns a promise for an array of values. If one | ||
of the given promise fails, the whole returned promise fails, not | ||
waiting for the rest of the batch. If you want to wait for all of the | ||
promises to either be fulfilled or rejected, you can use | ||
``allResolved``. | ||
The ``all`` function returns a promise for an array of values. When this | ||
promise is fulfilled, the array contains the fulfillment values of the original | ||
promises, in the same order as those promises. If one of the given promises | ||
is rejected, the returned promise is immediately rejected, not waiting for the | ||
rest of the batch. If you want to wait for all of the promises to either be | ||
fulfilled or rejected, you can use ``allResolved``. | ||
@@ -338,3 +339,8 @@ ```javascript | ||
Or, you could use th ultra-compact version: | ||
```javascript | ||
return funcs.reduce(Q.when, Q()); | ||
``` | ||
### Handling Errors | ||
@@ -637,5 +643,5 @@ | ||
round-trips by using these functions instead of ``then``. To take | ||
advantage of promises for remote objects, check out [Q-Comm][]. | ||
advantage of promises for remote objects, check out [Q-Connection][]. | ||
[Q-Comm]: https://github.com/kriskowal/q-comm | ||
[Q-Connection]: https://github.com/kriskowal/q-connection | ||
@@ -776,2 +782,8 @@ Even in the case of non-remote objects, these methods can be used as | ||
## Tests | ||
You can view the results of the Q test suite [in your browser][tests]! | ||
[tests]: https://rawgithub.com/kriskowal/q/master/spec/q-spec.html | ||
--- | ||
@@ -778,0 +790,0 @@ |
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
76503
8
1461
790