Comparing version 0.2.9 to 0.3.0
124
lib/q.js
@@ -157,4 +157,2 @@ // vim:ts=4:sts=4:sw=4: | ||
exports.makePromise = Promise; | ||
exports.Promise = Promise; // deprecated | ||
function Promise(descriptor, fallback, valueOf) { | ||
@@ -188,4 +186,4 @@ | ||
// provide thenables, CommonJS/Promises/A | ||
Promise.prototype.then = function (resolved, rejected) { | ||
return when(this, resolved, rejected); | ||
Promise.prototype.then = function (fulfilled, rejected) { | ||
return when(this, fulfilled, rejected); | ||
}; | ||
@@ -205,3 +203,3 @@ | ||
* @returns whether the given object is a promise. | ||
* Otherwise it is a resolved value. | ||
* Otherwise it is a fulfilled value. | ||
*/ | ||
@@ -214,4 +212,3 @@ exports.isPromise = isPromise; | ||
/** | ||
* @returns whether the given object is a fully | ||
* resolved value. | ||
* @returns whether the given object is a resolved promise. | ||
*/ | ||
@@ -222,9 +219,19 @@ exports.isResolved = isResolved; | ||
return true; | ||
return !isRejected(object) && !isPromise(object.valueOf()); | ||
return !isPromise(object.valueOf()); | ||
}; | ||
/** | ||
* @returns whether the given object is a | ||
* rejected promise. | ||
* @returns whether the given object is a value or fulfilled | ||
* promise. | ||
*/ | ||
exports.isFulfilled = isFulfilled; | ||
function isFulfilled(object) { | ||
if (object === undefined || object === null) | ||
return true; | ||
return !isPromise(object.valueOf()) && !isRejected(object); | ||
}; | ||
/** | ||
* @returns whether the given object is a rejected promise. | ||
*/ | ||
exports.isRejected = isRejected; | ||
@@ -307,4 +314,3 @@ function isRejected(object) { | ||
}, | ||
"post": function (name /*...args*/) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
"post": function (name, value) { | ||
if (object === undefined || object === null) | ||
@@ -317,3 +323,3 @@ return reject("" + object + " has no methods"); | ||
return reject("Property " + name + " on object " + object + " is not a method"); | ||
return object[name].apply(object, args); | ||
return object[name].apply(object, value); | ||
}, | ||
@@ -354,9 +360,9 @@ "keys": function () { | ||
* | ||
* 1. that resolved and rejected will be called only once. | ||
* 2. that either the resolved callback or the rejected callback will be | ||
* 1. that fulfilled and rejected will be called only once. | ||
* 2. that either the fulfilled callback or the rejected callback will be | ||
* called, but not both. | ||
* 3. that resolved and rejected will not be called in this turn. | ||
* 3. that fulfilled and rejected will not be called in this turn. | ||
* | ||
* @param value promise or immediate reference to observe | ||
* @param resolve function to be called with the resolved value | ||
* @param fulfilled function to be called with the fulfilled value | ||
* @param rejected function to be called with the rejection reason | ||
@@ -366,3 +372,3 @@ * @return promise for the return value from the invoked callback | ||
exports.when = when; | ||
function when(value, resolved, rejected) { | ||
function when(value, fulfilled, rejected) { | ||
var deferred = defer(); | ||
@@ -372,5 +378,5 @@ var done = false; // ensure the untrusted promise makes at most a | ||
function _resolved(value) { | ||
function _fulfilled(value) { | ||
try { | ||
return resolved ? resolved(value) : value; | ||
return fulfilled ? fulfilled(value) : value; | ||
} catch (exception) { | ||
@@ -399,3 +405,3 @@ if (typeof process !== "undefined") { | ||
done = true; | ||
deferred.resolve(ref(value)[DUCK]("when", _resolved, _rejected)); | ||
deferred.resolve(ref(value)[DUCK]("when", _fulfilled, _rejected)); | ||
}, function (reason) { | ||
@@ -411,27 +417,28 @@ if (done) | ||
/** | ||
* Like "when", but attempts to return a fully resolved | ||
* value in the same turn. If the given value is fully | ||
* resolved, and the value returned by the resolved | ||
* callback is fully resolved, asap returns the latter | ||
* value in the same turn. Otherwise, it returns a promise | ||
* that will be resolved in a future turn. | ||
* Like "when", but attempts to return a fulfilled value in | ||
* the same turn. If the given value is fulfilled and the | ||
* value returned by the fulfilled callback is fulfilled, | ||
* asap returns the latter value in the same turn. | ||
* Otherwise, it returns a promise that will be resolved in | ||
* a future turn. | ||
* | ||
* This method is an experiment in providing an API | ||
* that can unify synchronous and asynchronous API's. | ||
* An API that uses "asap" guarantees that, if it | ||
* is provided fully resolved values, it would produce | ||
* fully resolved values, but if it is provided | ||
* asynchronous promises, it will produce asynchronous | ||
* promises. | ||
* This method is an experiment in providing an API that can | ||
* unify synchronous and asynchronous API's. An API that | ||
* uses "asap" guarantees that, if it is provided fully | ||
* resolved values, it will produce fully resolved values or | ||
* throw exceptions, but if it is provided asynchronous | ||
* promises, it will produce asynchronous promises. | ||
* | ||
* /!\ WARNING: this method is experimental and likely | ||
* to be removed on the grounds that it probably | ||
* will result in composition hazards. | ||
* /!\ WARNING: this method is experimental and likely to be | ||
* removed on the grounds that it probably will result in | ||
* composition hazards. | ||
*/ | ||
exports.asap = function (value, resolved, rejected) { | ||
resolved = resolved || identity; | ||
if (isResolved(value)) | ||
return resolved(value.valueOf()).valueOf(); | ||
exports.asap = function (value, fulfilled, rejected) { | ||
fulfilled = fulfilled || identity; | ||
if (isFulfilled(value)) | ||
return fulfilled(value.valueOf()).valueOf(); | ||
else if (isRejected(value)) | ||
throw value.valueOf().reason; | ||
else | ||
return when(value, resolved, rejected); | ||
return when(value, fulfilled, rejected); | ||
}; | ||
@@ -501,7 +508,23 @@ | ||
* @param name name of method to invoke | ||
* @param value a value to post, typically an array of | ||
* invocation arguments for promises that | ||
* are ultimately backed with `ref` values, | ||
* as opposed to those backed with URLs | ||
* wherein the posted value can be any | ||
* JSON serializable object. | ||
* @return promise for the return value | ||
*/ | ||
var post = exports.post = Method("post"); | ||
/** | ||
* Invokes a method in a future turn. | ||
* @param object promise or immediate reference for target object | ||
* @param name name of method to invoke | ||
* @param ...args array of invocation arguments | ||
* @return promise for the return value | ||
*/ | ||
exports.invoke = | ||
exports.post = Method("post"); | ||
exports.invoke = function (value, name) { | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
return post(value, name, args); | ||
}; | ||
@@ -517,18 +540,5 @@ /** | ||
/** | ||
* Guarantees that the give promise resolves to a defined, non-null value. | ||
*/ | ||
exports.defined = function (value) { | ||
return exports.when(value, function (value) { | ||
if (value === undefined || value === null) | ||
return reject("Resolved undefined value: " + value); | ||
return value; | ||
}); | ||
}; | ||
/** | ||
* Throws an error with the given reason. | ||
*/ | ||
exports.error = function (reason) { | ||
if (!(reason instanceof Error)) | ||
reason = new Error(reason); | ||
throw reason; | ||
@@ -535,0 +545,0 @@ }; |
@@ -14,2 +14,37 @@ (function (require, exports) { | ||
exports.join = function () { | ||
var values = Array.prototype.slice.call(arguments); | ||
var fulfilled = values.pop(); | ||
var reasons; | ||
var fulfillment = Q.defer(); | ||
var completion = values.reduce(function (done, value, i) { | ||
return Q.when(done, function () { | ||
return Q.when(value, function (value) { | ||
values[i] = value; | ||
}, function (reason) { | ||
reasons = reasons || []; | ||
reasons[i] = reason; | ||
fulfillment.reject(reason); | ||
}); | ||
}); | ||
}, undefined); | ||
Q.when(completion, fulfillment.resolve); | ||
return Q.when(fulfillment.promise, function () { | ||
return fulfilled ? fulfilled.apply(null, values) : values; | ||
}, function () { | ||
reasons = reasons || []; | ||
return Q.reject({ | ||
"toString": function () { | ||
return "Can't join. " + reasons.join("; "); | ||
}, | ||
"reasons": Q.when(completion, function () { | ||
return reasons; | ||
}), | ||
"stack": reasons.reduce(function (prev, next) { | ||
return prev || next; | ||
}).stack | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -16,0 +51,0 @@ * Calls each step function serially, proceeding only when |
{ | ||
"name": "q", | ||
"description": "defer/when-style promises (CommonJS/Promises/A,B,D)", | ||
"version": "0.2.9", | ||
"version": "0.3.0", | ||
"homepage": "http://github.com/kriskowal/q/", | ||
@@ -44,2 +44,10 @@ "author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)", | ||
} | ||
}, | ||
"browser": { | ||
"dependencies": {} | ||
}, | ||
"lode": { | ||
"directories": { | ||
"lib": "lib/q" | ||
} | ||
} | ||
@@ -46,0 +54,0 @@ }, |
// vim:ts=4:sts=4:sw=4: | ||
'use stirct'; | ||
'use strict'; | ||
@@ -65,2 +65,23 @@ var Q = require('q'); | ||
Q.when(Q.post(d.promise, 'a', [1]), function (result) { | ||
ASSERT.ok(result === 2, 'correct value is returned by post'); | ||
ASSERT.ok(value._a === 1, 'post invoked function as expected'); | ||
done(); | ||
}, function (reason) { | ||
ASSERT.fail(reason); | ||
done(); | ||
}); | ||
d.resolve(value); | ||
}; | ||
exports['test invoke resolved'] = function (ASSERT, done) { | ||
var d = Q.defer(); | ||
var value = { | ||
_a: null, | ||
a: function a(value) { | ||
this._a = value; | ||
return 1 + value; | ||
} | ||
}; | ||
Q.when(Q.invoke(d.promise, 'a', 1), function (result) { | ||
@@ -135,1 +156,2 @@ ASSERT.ok(result === 2, 'correct value is returned by invoke'); | ||
require('test').run(exports) | ||
@@ -24,3 +24,3 @@ 'use strict' | ||
, function(value) { | ||
resoved = true | ||
resolved = true | ||
return value | ||
@@ -31,3 +31,3 @@ } | ||
, function(value) { | ||
assert.equal(value, resolution, 'value resoved as expected') | ||
assert.equal(value, resolution, 'value resolved as expected') | ||
assert.ok(nextTurn, 'callback is called in next turn of event loop') | ||
@@ -34,0 +34,0 @@ done() |
var Q = require('q'); | ||
exports['test reject: isRejected, isResolved'] = function (ASSERT, done) { | ||
exports['test reject: isRejected, isResolved, isFulfilled'] = function (ASSERT, done) { | ||
var future = false; | ||
@@ -9,3 +9,4 @@ var reason = {}; | ||
ASSERT.ok(Q.isRejected(rejection), 'should be rejected in current turn'); | ||
ASSERT.ok(!Q.isResolved(rejection), 'should not be resolved in current turn'); | ||
ASSERT.ok(!Q.isFulfilled(rejection), 'should not be fulfilled in current turn'); | ||
ASSERT.ok(Q.isResolved(rejection), 'should be resolved in current turn'); | ||
Q.when(function () { | ||
@@ -12,0 +13,0 @@ ASSERT.ok(false, 'should not be resolved in a future turn'); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
85648
1668
643
0
36