Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "q", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A library for promises (CommonJS/Promises/A,B,D)", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/kriskowal/q", |
85
q.js
@@ -30,4 +30,3 @@ // vim:ts=4:sts=4:sw=4: | ||
(function (definition) { | ||
// Turn off strict mode for this function so we can assign to global.Q | ||
/* jshint strict: false */ | ||
"use strict"; | ||
@@ -44,3 +43,3 @@ // This file will function properly as a <script> tag, or a module | ||
// CommonJS | ||
} else if (typeof exports === "object") { | ||
} else if (typeof exports === "object" && typeof module === "object") { | ||
module.exports = definition(); | ||
@@ -61,4 +60,7 @@ | ||
// <script> | ||
} else if (typeof window !== "undefined") { | ||
window.Q = Q = definition(); | ||
} else { | ||
Q = definition(); | ||
throw new Error("This environment was not anticiapted by Q. Please file a bug."); | ||
} | ||
@@ -456,3 +458,3 @@ | ||
// but to tolerably coerce non-promises to promises. | ||
if (isPromise(value)) { | ||
if (value instanceof Promise) { | ||
return value; | ||
@@ -481,2 +483,7 @@ } | ||
// enable long stacks if Q_DEBUG is set | ||
if (typeof process === "object" && process && process.env && process.env.Q_DEBUG) { | ||
Q.longStackSupport = true; | ||
} | ||
/** | ||
@@ -513,3 +520,3 @@ * Constructs a {promise, resolve, reject} object. | ||
} else { | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
resolvedPromise.promiseDispatch.apply(resolvedPromise, args); | ||
@@ -562,3 +569,3 @@ }); | ||
array_reduce(messages, function (undefined, message) { | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
newPromise.promiseDispatch.apply(newPromise, message); | ||
@@ -601,3 +608,3 @@ }); | ||
array_reduce(progressListeners, function (undefined, progressListener) { | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
progressListener(progress); | ||
@@ -695,5 +702,5 @@ }); | ||
/** | ||
* Returns a promise for the first of an array of promises to become fulfilled. | ||
* Returns a promise for the first of an array of promises to become settled. | ||
* @param answers {Array[Any*]} promises to race | ||
* @returns {Any*} the first promise to be fulfilled | ||
* @returns {Any*} the first promise to be settled | ||
*/ | ||
@@ -818,3 +825,3 @@ Q.race = race; | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
self.promiseDispatch(function (value) { | ||
@@ -860,3 +867,27 @@ if (done) { | ||
Q.tap = function (promise, callback) { | ||
return Q(promise).tap(callback); | ||
}; | ||
/** | ||
* Works almost like "finally", but not called for rejections. | ||
* Original resolution value is passed through callback unaffected. | ||
* Callback may return a promise that will be awaited for. | ||
* @param {Function} callback | ||
* @returns {Q.Promise} | ||
* @example | ||
* doSomething() | ||
* .then(...) | ||
* .tap(console.log) | ||
* .then(...); | ||
*/ | ||
Promise.prototype.tap = function (callback) { | ||
callback = Q(callback); | ||
return this.then(function (value) { | ||
return callback.fcall(value).thenResolve(value); | ||
}); | ||
}; | ||
/** | ||
* Registers an observer on a promise. | ||
@@ -926,5 +957,3 @@ * | ||
function isPromise(object) { | ||
return isObject(object) && | ||
typeof object.promiseDispatch === "function" && | ||
typeof object.inspect === "function"; | ||
return object instanceof Promise; | ||
} | ||
@@ -1107,3 +1136,3 @@ | ||
var deferred = defer(); | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
try { | ||
@@ -1209,3 +1238,3 @@ promise.then(deferred.resolve, deferred.reject, deferred.notify); | ||
if (result.done) { | ||
return result.value; | ||
return Q(result.value); | ||
} else { | ||
@@ -1221,3 +1250,3 @@ return when(result.value, callback, errback); | ||
if (isStopIteration(exception)) { | ||
return exception.value; | ||
return Q(exception.value); | ||
} else { | ||
@@ -1318,3 +1347,3 @@ return reject(exception); | ||
var deferred = defer(); | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
self.promiseDispatch(deferred.resolve, op, args); | ||
@@ -1662,3 +1691,3 @@ }); | ||
// does not catch it and turn it into a rejection. | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
makeStackTraceLong(error, promise); | ||
@@ -1690,14 +1719,18 @@ if (Q.onerror) { | ||
* @param {Number} milliseconds timeout | ||
* @param {String} custom error message (optional) | ||
* @param {Any*} custom error message or Error object (optional) | ||
* @returns a promise for the resolution of the given promise if it is | ||
* fulfilled before the timeout, otherwise rejected. | ||
*/ | ||
Q.timeout = function (object, ms, message) { | ||
return Q(object).timeout(ms, message); | ||
Q.timeout = function (object, ms, error) { | ||
return Q(object).timeout(ms, error); | ||
}; | ||
Promise.prototype.timeout = function (ms, message) { | ||
Promise.prototype.timeout = function (ms, error) { | ||
var deferred = defer(); | ||
var timeoutId = setTimeout(function () { | ||
deferred.reject(new Error(message || "Timed out after " + ms + " ms")); | ||
if (!error || "string" === typeof error) { | ||
error = new Error(error || "Timed out after " + ms + " ms"); | ||
error.code = "ETIMEDOUT"; | ||
} | ||
deferred.reject(error); | ||
}, ms); | ||
@@ -1904,7 +1937,7 @@ | ||
this.then(function (value) { | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
nodeback(null, value); | ||
}); | ||
}, function (error) { | ||
nextTick(function () { | ||
Q.nextTick(function () { | ||
nodeback(error); | ||
@@ -1911,0 +1944,0 @@ }); |
[![Build Status](https://secure.travis-ci.org/kriskowal/q.png?branch=master)](http://travis-ci.org/kriskowal/q) | ||
<a href="http://promises-aplus.github.com/promises-spec"> | ||
<img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" | ||
align="right" alt="Promises/A+ logo" /> | ||
<img src="http://kriskowal.github.io/q/q.png" | ||
align="right" alt="Q logo" /> | ||
</a> | ||
@@ -12,3 +12,3 @@ | ||
equivalent to version 1.0.0. All further releases of `q@~1.0` will be backward | ||
compatible. The version 2 release train introduces significant but | ||
compatible. The version 2 release train introduces significant and | ||
backward-incompatible changes and is experimental at this time.* | ||
@@ -84,3 +84,3 @@ | ||
- A [component](https://github.com/component/component) as ``microjs/q`` | ||
- Using [bower](http://bower.io/) as ``q`` | ||
- Using [bower](http://bower.io/) as `q#1.0.1` | ||
- Using [NuGet](http://nuget.org/) as [Q](https://nuget.org/packages/q) | ||
@@ -299,3 +299,3 @@ | ||
will get called at the first sign of failure. That is, whichever of | ||
the recived promises fails first gets handled by the rejection handler. | ||
the received promises fails first gets handled by the rejection handler. | ||
@@ -372,6 +372,6 @@ ```javascript | ||
Or, you could use th ultra-compact version: | ||
Or, you could use the ultra-compact version: | ||
```javascript | ||
return funcs.reduce(Q.when, Q()); | ||
return funcs.reduce(Q.when, Q(initialVal)); | ||
``` | ||
@@ -382,3 +382,3 @@ | ||
One sometimes-unintuive aspect of promises is that if you throw an | ||
exception in the fulfillment handler, it will not be be caught by the error | ||
exception in the fulfillment handler, it will not be caught by the error | ||
handler. | ||
@@ -618,2 +618,42 @@ | ||
#### Using `Q.Promise` | ||
This is an alternative promise-creation API that has the same power as | ||
the deferred concept, but without introducing another conceptual entity. | ||
Rewriting the `requestOkText` example above using `Q.Promise`: | ||
```javascript | ||
function requestOkText(url) { | ||
return Q.Promise(function(resolve, reject, notify) { | ||
var request = new XMLHttpRequest(); | ||
request.open("GET", url, true); | ||
request.onload = onload; | ||
request.onerror = onerror; | ||
request.onprogress = onprogress; | ||
request.send(); | ||
function onload() { | ||
if (request.status === 200) { | ||
resolve(request.responseText); | ||
} else { | ||
reject(new Error("Status code was " + request.status)); | ||
} | ||
} | ||
function onerror() { | ||
reject(new Error("Can't XHR " + JSON.stringify(url))); | ||
} | ||
function onprogress(event) { | ||
notify(event.loaded / event.total); | ||
} | ||
}); | ||
} | ||
``` | ||
If `requestOkText` were to throw an exception, the returned promise would be | ||
rejected with that thrown exception as the rejection reason. | ||
### The Middle | ||
@@ -807,3 +847,3 @@ | ||
Note how you can see the the function that triggered the async operation in the | ||
Note how you can see the function that triggered the async operation in the | ||
stack trace! This is very helpful for debugging, as otherwise you end up getting | ||
@@ -813,2 +853,11 @@ only the first line, plus a bunch of Q internals, with no sign of where the | ||
In node.js, this feature can also be enabled through the Q_DEBUG environment | ||
variable: | ||
``` | ||
Q_DEBUG=1 node server.js | ||
``` | ||
This will enable long stack support in every instance of Q. | ||
This feature does come with somewhat-serious performance and memory overhead, | ||
@@ -815,0 +864,0 @@ however. If you're working with lots of promises, or trying to scale a server |
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
134631
10
1836
870