Comparing version 0.13.3 to 0.13.4
@@ -35,4 +35,5 @@ (function (global) { | ||
if (!global.preload) { | ||
var mrLocation = resolve(window.location, params.mrLocation); | ||
for (var id in pending) { | ||
load(pending[id]); | ||
load(resolve(mrLocation, pending[id])); | ||
} | ||
@@ -180,5 +181,4 @@ } | ||
var load = function (location) { | ||
var params = getParams(); | ||
var script = document.createElement("script"); | ||
script.src = resolve(params.mrLocation, location); | ||
script.src = location; | ||
script.onload = function () { | ||
@@ -185,0 +185,0 @@ // remove clutter |
@@ -144,2 +144,23 @@ /* <copyright> | ||
}; | ||
var loadIfNotPreloaded = function (location, definition, preloaded) { | ||
// The package.json might come in a preloading bundle. If so, we do not | ||
// want to issue a script injection. However, if by the time preloading | ||
// has finished the package.json has not arrived, we will need to kick off | ||
// a request for the requested script. | ||
if (preloaded && preloaded.isPending()) { | ||
preloaded | ||
.then(function () { | ||
if (definition.isPending()) { | ||
Require.loadScript(location); | ||
} | ||
}) | ||
.done(); | ||
} else if (definition.isPending()) { | ||
// otherwise preloading has already completed and we don't have the | ||
// module, so load it | ||
Require.loadScript(location); | ||
} | ||
}; | ||
// global | ||
@@ -179,5 +200,6 @@ montageDefine = function (hash, id, module) { | ||
Require.loadScript(location); | ||
var definition = getDefinition(hash, module.id).promise; | ||
loadIfNotPreloaded(location, definition, config.preloaded); | ||
return getDefinition(hash, module.id).promise; | ||
return definition; | ||
}) | ||
@@ -204,19 +226,3 @@ .then(function (definition) { | ||
// The package.json might come in a preloading bundle. If so, we do not | ||
// want to issue a script injection. However, if by the time preloading | ||
// has finished the package.json has not arrived, we will need to kick off | ||
// a request for the package.json.load.js script. | ||
if (config.preloaded && config.preloaded.isPending()) { | ||
config.preloaded | ||
.then(function () { | ||
if (definition.isPending()) { | ||
Require.loadScript(location); | ||
} | ||
}) | ||
.done(); | ||
} else if (definition.isPending()) { | ||
// otherwise preloading has already completed and we don't have the | ||
// package description, so load it | ||
Require.loadScript(location); | ||
} | ||
loadIfNotPreloaded(location, definition, config.preloaded); | ||
@@ -223,0 +229,0 @@ return definition.get("exports"); |
@@ -0,1 +1,8 @@ | ||
### 0.13.4 | ||
- Update Q from v0.9.6 to v0.9.7 | ||
- Fix loading of bundles | ||
- Wait for preload to finish before issuing requests for modules that might | ||
be included in one of the bundles | ||
### 0.13.3 | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "mr", | ||
"version": "0.13.3", | ||
"version": "0.13.4", | ||
"description": "A refresh-only CommonJS module system for browsers, used in Montage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
{ | ||
"name": "q", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "A library for promises (CommonJS/Promises/A,B,D)", | ||
@@ -30,8 +30,6 @@ "homepage": "https://github.com/kriskowal/q", | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/kriskowal/q/raw/master/LICENSE" | ||
} | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "http://github.com/kriskowal/q/raw/master/LICENSE" | ||
}, | ||
"main": "q.js", | ||
@@ -48,5 +46,5 @@ "repository": { | ||
"devDependencies": { | ||
"jshint": "~2.1.3", | ||
"jshint": "~2.1.9", | ||
"cover": "*", | ||
"jasmine-node": "1.2.2", | ||
"jasmine-node": "1.11.0", | ||
"opener": "*", | ||
@@ -56,3 +54,4 @@ "promises-aplus-tests": "1.x", | ||
"grunt-cli": "~0.1.9", | ||
"grunt-contrib-uglify": "~0.2.2" | ||
"grunt-contrib-uglify": "~0.2.2", | ||
"matcha": "~0.2.0" | ||
}, | ||
@@ -62,2 +61,3 @@ "scripts": { | ||
"test-browser": "opener spec/q-spec.html", | ||
"benchmark": "matcha", | ||
"lint": "jshint q.js", | ||
@@ -64,0 +64,0 @@ "cover": "cover run node_modules/jasmine-node/bin/jasmine-node spec && cover report html && opener cover_html/index.html", |
@@ -94,2 +94,4 @@ // vim:ts=4:sts=4:sw=4: | ||
function flush() { | ||
/* jshint loopfunc: true */ | ||
while (head.next) { | ||
@@ -117,5 +119,9 @@ head = head.next; | ||
// Continue in next event to avoid tick recursion. | ||
domain && domain.exit(); | ||
if (domain) { | ||
domain.exit(); | ||
} | ||
setTimeout(flush, 0); | ||
domain && domain.enter(); | ||
if (domain) { | ||
domain.enter(); | ||
} | ||
@@ -177,6 +183,18 @@ throw e; | ||
var channel = new MessageChannel(); | ||
channel.port1.onmessage = flush; | ||
requestTick = function () { | ||
// At least Safari Version 6.0.5 (8536.30.1) intermittently cannot create | ||
// working message ports the first time a page loads. | ||
channel.port1.onmessage = function () { | ||
requestTick = requestPortTick; | ||
channel.port1.onmessage = flush; | ||
flush(); | ||
}; | ||
var requestPortTick = function () { | ||
// Opera requires us to provide a message payload, regardless of | ||
// whether we use it. | ||
channel.port2.postMessage(0); | ||
}; | ||
requestTick = function () { | ||
setTimeout(flush, 0); | ||
requestPortTick(); | ||
}; | ||
@@ -204,4 +222,4 @@ } else { | ||
// http://wiki.ecmascript.org/doku.php?id=conventions:safe_meta_programming | ||
var call = Function.call; | ||
function uncurryThis(f) { | ||
var call = Function.call; | ||
return function () { | ||
@@ -448,9 +466,22 @@ return call.apply(f, arguments); | ||
/** | ||
* Creates fulfilled promises from non-thenables, | ||
* Passes Q promises through, | ||
* Coerces other thenables to Q promises. | ||
* Constructs a promise for an immediate reference, passes promises through, or | ||
* coerces promises from different systems. | ||
* @param value immediate reference or promise | ||
*/ | ||
function Q(value) { | ||
return resolve(value); | ||
// If the object is already a Promise, return it directly. This enables | ||
// the resolve function to both be used to created references from objects, | ||
// but to tolerably coerce non-promises to promises. | ||
if (isPromise(value)) { | ||
return value; | ||
} | ||
// assimilate thenables | ||
if (isPromiseAlike(value)) { | ||
return coerce(value); | ||
} else { | ||
return fulfill(value); | ||
} | ||
} | ||
Q.resolve = Q; | ||
@@ -562,3 +593,3 @@ /** | ||
become(resolve(value)); | ||
become(Q(value)); | ||
}; | ||
@@ -624,14 +655,74 @@ | ||
} | ||
var deferred = defer(); | ||
fcall( | ||
resolver, | ||
deferred.resolve, | ||
deferred.reject, | ||
deferred.notify | ||
).fail(deferred.reject); | ||
try { | ||
resolver(deferred.resolve, deferred.reject, deferred.notify); | ||
} catch (reason) { | ||
deferred.reject(reason); | ||
} | ||
return deferred.promise; | ||
} | ||
// XXX experimental. This method is a way to denote that a local value is | ||
// serializable and should be immediately dispatched to a remote upon request, | ||
// instead of passing a reference. | ||
Q.passByCopy = function (object) { | ||
//freeze(object); | ||
//passByCopies.set(object, true); | ||
return object; | ||
}; | ||
Promise.prototype.passByCopy = function () { | ||
//freeze(object); | ||
//passByCopies.set(object, true); | ||
return this; | ||
}; | ||
/** | ||
* If two promises eventually fulfill to the same value, promises that value, | ||
* but otherwise rejects. | ||
* @param x {Any*} | ||
* @param y {Any*} | ||
* @returns {Any*} a promise for x and y if they are the same, but a rejection | ||
* otherwise. | ||
* | ||
*/ | ||
Q.join = function (x, y) { | ||
return Q(x).join(y); | ||
}; | ||
Promise.prototype.join = function (that) { | ||
return Q([this, that]).spread(function (x, y) { | ||
if (x === y) { | ||
// TODO: "===" should be Object.is or equiv | ||
return x; | ||
} else { | ||
throw new Error("Can't join: not the same: " + x + " " + y); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Returns a promise for the first of an array of promises to become fulfilled. | ||
* @param answers {Array[Any*]} promises to race | ||
* @returns {Any*} the first promise to be fulfilled | ||
*/ | ||
Q.race = race; | ||
function race(answerPs) { | ||
return promise(function(resolve, reject) { | ||
// Switch to this once we can assume at least ES5 | ||
// answerPs.forEach(function(answerP) { | ||
// Q(answerP).then(resolve, reject); | ||
// }); | ||
// Use this in the meantime | ||
for (var i = 0, len = answerPs.length; i < len; i++) { | ||
Q(answerPs[i]).then(resolve, reject); | ||
} | ||
}); | ||
} | ||
Promise.prototype.race = function () { | ||
return this.then(Q.race); | ||
}; | ||
/** | ||
* Constructs a Promise with a promise descriptor object and optional fallback | ||
@@ -702,2 +793,6 @@ * function. The descriptor contains methods like when(rejected), get(name), | ||
Promise.prototype.toString = function () { | ||
return "[object Promise]"; | ||
}; | ||
Promise.prototype.then = function (fulfilled, rejected, progressed) { | ||
@@ -774,44 +869,37 @@ var self = this; | ||
/** | ||
* Registers an observer on a promise. | ||
* | ||
* Guarantees: | ||
* | ||
* 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 fulfilled and rejected will not be called in this turn. | ||
* | ||
* @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 | ||
*/ | ||
Q.when = when; | ||
function when(value, fulfilled, rejected, progressed) { | ||
return Q(value).then(fulfilled, rejected, progressed); | ||
} | ||
Promise.prototype.thenResolve = function (value) { | ||
return when(this, function () { return value; }); | ||
return this.then(function () { return value; }); | ||
}; | ||
Promise.prototype.thenReject = function (reason) { | ||
return when(this, function () { throw reason; }); | ||
Q.thenResolve = function (promise, value) { | ||
return Q(promise).thenResolve(value); | ||
}; | ||
// Chainable methods | ||
array_reduce( | ||
[ | ||
"isFulfilled", "isRejected", "isPending", | ||
"dispatch", | ||
"when", "spread", | ||
"get", "set", "del", "delete", | ||
"post", "send", "mapply", "invoke", "mcall", | ||
"keys", | ||
"fapply", "fcall", "fbind", | ||
"all", "allResolved", | ||
"timeout", "delay", | ||
"catch", "finally", "fail", "fin", "progress", "done", | ||
"nfcall", "nfapply", "nfbind", "denodeify", "nbind", | ||
"npost", "nsend", "nmapply", "ninvoke", "nmcall", | ||
"nodeify" | ||
], | ||
function (undefined, name) { | ||
Promise.prototype[name] = function () { | ||
return Q[name].apply( | ||
Q, | ||
[this].concat(array_slice(arguments)) | ||
); | ||
}; | ||
}, | ||
void 0 | ||
); | ||
Promise.prototype.toSource = function () { | ||
return this.toString(); | ||
Promise.prototype.thenReject = function (reason) { | ||
return this.then(function () { throw reason; }); | ||
}; | ||
Promise.prototype.toString = function () { | ||
return "[object Promise]"; | ||
Q.thenReject = function (promise, reason) { | ||
return Q(promise).thenReject(reason); | ||
}; | ||
@@ -866,2 +954,6 @@ | ||
Promise.prototype.isPending = function () { | ||
return this.inspect().state === "pending"; | ||
}; | ||
/** | ||
@@ -876,2 +968,6 @@ * @returns whether the given object is a value or fulfilled | ||
Promise.prototype.isFulfilled = function () { | ||
return this.inspect().state === "fulfilled"; | ||
}; | ||
/** | ||
@@ -885,2 +981,6 @@ * @returns whether the given object is a rejected promise. | ||
Promise.prototype.isRejected = function () { | ||
return this.inspect().state === "rejected"; | ||
}; | ||
//// BEGIN UNHANDLED REJECTION TRACKING | ||
@@ -913,7 +1013,3 @@ | ||
var reason = unhandledReasons[i]; | ||
if (reason && typeof reason.stack !== "undefined") { | ||
console.warn("Unhandled rejection reason:", reason.stack); | ||
} else { | ||
console.warn("Unhandled rejection reason (no stack):", reason); | ||
} | ||
console.warn("Unhandled rejection reason:", reason); | ||
} | ||
@@ -945,3 +1041,7 @@ } | ||
unhandledRejections.push(promise); | ||
unhandledReasons.push(reason); | ||
if (reason && typeof reason.stack !== "undefined") { | ||
unhandledReasons.push(reason.stack); | ||
} else { | ||
unhandledReasons.push("(no stack) " + reason); | ||
} | ||
displayUnhandledReasons(); | ||
@@ -1035,4 +1135,4 @@ } | ||
}, | ||
"apply": function (thisP, args) { | ||
return value.apply(thisP, args); | ||
"apply": function (thisp, args) { | ||
return value.apply(thisp, args); | ||
}, | ||
@@ -1048,24 +1148,2 @@ "keys": function () { | ||
/** | ||
* Constructs a promise for an immediate reference, passes promises through, or | ||
* coerces promises from different systems. | ||
* @param value immediate reference or promise | ||
*/ | ||
Q.resolve = resolve; | ||
function resolve(value) { | ||
// If the object is already a Promise, return it directly. This enables | ||
// the resolve function to both be used to created references from objects, | ||
// but to tolerably coerce non-promises to promises. | ||
if (isPromise(value)) { | ||
return value; | ||
} | ||
// assimilate thenables | ||
if (isPromiseAlike(value)) { | ||
return coerce(value); | ||
} else { | ||
return fulfill(value); | ||
} | ||
} | ||
/** | ||
* Converts thenables to Q promises. | ||
@@ -1103,3 +1181,3 @@ * @param promise thenable promise | ||
}, function () { | ||
return resolve(object).inspect(); | ||
return Q(object).inspect(); | ||
}); | ||
@@ -1109,23 +1187,2 @@ } | ||
/** | ||
* Registers an observer on a promise. | ||
* | ||
* Guarantees: | ||
* | ||
* 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 fulfilled and rejected will not be called in this turn. | ||
* | ||
* @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 | ||
*/ | ||
Q.when = when; | ||
function when(value, fulfilled, rejected, progressed) { | ||
return Q(value).then(fulfilled, rejected, progressed); | ||
} | ||
/** | ||
* Spreads the values of a promised array of arguments into the | ||
@@ -1141,10 +1198,12 @@ * fulfillment callback. | ||
Q.spread = spread; | ||
function spread(promise, fulfilled, rejected) { | ||
return when(promise, function (valuesOrPromises) { | ||
return all(valuesOrPromises).then(function (values) { | ||
return fulfilled.apply(void 0, values); | ||
}, rejected); | ||
}, rejected); | ||
function spread(value, fulfilled, rejected) { | ||
return Q(value).spread(fulfilled, rejected); | ||
} | ||
Promise.prototype.spread = function (fulfilled, rejected) { | ||
return this.all().then(function (array) { | ||
return fulfilled.apply(void 0, array); | ||
}, rejected); | ||
}; | ||
/** | ||
@@ -1209,3 +1268,3 @@ * The async function is a decorator for generator functions, turning | ||
var generator = makeGenerator.apply(this, arguments); | ||
var callback = continuer.bind(continuer, "send"); | ||
var callback = continuer.bind(continuer, "next"); | ||
var errback = continuer.bind(continuer, "throw"); | ||
@@ -1268,3 +1327,3 @@ return callback(); | ||
* }); | ||
* add(Q.resolve(a), Q.resolve(B)); | ||
* add(Q(a), Q(B)); | ||
* | ||
@@ -1292,24 +1351,15 @@ * @param {function} callback The function to decorate | ||
function dispatch(object, op, args) { | ||
return Q(object).dispatch(op, args); | ||
} | ||
Promise.prototype.dispatch = function (op, args) { | ||
var self = this; | ||
var deferred = defer(); | ||
nextTick(function () { | ||
resolve(object).promiseDispatch(deferred.resolve, op, args); | ||
self.promiseDispatch(deferred.resolve, op, args); | ||
}); | ||
return deferred.promise; | ||
} | ||
}; | ||
/** | ||
* Constructs a promise method that can be used to safely observe resolution of | ||
* a promise for an arbitrarily named method like "propfind" in a future turn. | ||
* | ||
* "dispatcher" constructs methods like "get(promise, name)" and "set(promise)". | ||
*/ | ||
Q.dispatcher = dispatcher; | ||
function dispatcher(op) { | ||
return function (object) { | ||
var args = array_slice(arguments, 1); | ||
return dispatch(object, op, args); | ||
}; | ||
} | ||
/** | ||
* Gets the value of a property in a future turn. | ||
@@ -1320,4 +1370,10 @@ * @param object promise or immediate reference for target object | ||
*/ | ||
Q.get = dispatcher("get"); | ||
Q.get = function (object, key) { | ||
return Q(object).dispatch("get", [key]); | ||
}; | ||
Promise.prototype.get = function (key) { | ||
return this.dispatch("get", [key]); | ||
}; | ||
/** | ||
@@ -1330,4 +1386,10 @@ * Sets the value of a property in a future turn. | ||
*/ | ||
Q.set = dispatcher("set"); | ||
Q.set = function (object, key, value) { | ||
return Q(object).dispatch("set", [key, value]); | ||
}; | ||
Promise.prototype.set = function (key, value) { | ||
return this.dispatch("set", [key, value]); | ||
}; | ||
/** | ||
@@ -1339,5 +1401,12 @@ * Deletes a property in a future turn. | ||
*/ | ||
Q["delete"] = // XXX experimental | ||
Q.del = dispatcher("delete"); | ||
Q.del = // XXX legacy | ||
Q["delete"] = function (object, key) { | ||
return Q(object).dispatch("delete", [key]); | ||
}; | ||
Promise.prototype.del = // XXX legacy | ||
Promise.prototype["delete"] = function (key) { | ||
return this.dispatch("delete", [key]); | ||
}; | ||
/** | ||
@@ -1356,5 +1425,12 @@ * Invokes a method in a future turn. | ||
// bound locally because it is used by other methods | ||
var post = Q.post = dispatcher("post"); | ||
Q.mapply = post; // experimental | ||
Q.mapply = // XXX As proposed by "Redsandro" | ||
Q.post = function (object, name, args) { | ||
return Q(object).dispatch("post", [name, args]); | ||
}; | ||
Promise.prototype.mapply = // XXX As proposed by "Redsandro" | ||
Promise.prototype.post = function (name, args) { | ||
return this.dispatch("post", [name, args]); | ||
}; | ||
/** | ||
@@ -1367,10 +1443,14 @@ * Invokes a method in a future turn. | ||
*/ | ||
Q.send = send; | ||
Q.invoke = send; // synonyms | ||
Q.mcall = send; // experimental | ||
function send(value, name) { | ||
var args = array_slice(arguments, 2); | ||
return post(value, name, args); | ||
} | ||
Q.send = // XXX Mark Miller's proposed parlance | ||
Q.mcall = // XXX As proposed by "Redsandro" | ||
Q.invoke = function (object, name /*...args*/) { | ||
return Q(object).dispatch("post", [name, array_slice(arguments, 2)]); | ||
}; | ||
Promise.prototype.send = // XXX Mark Miller's proposed parlance | ||
Promise.prototype.mcall = // XXX As proposed by "Redsandro" | ||
Promise.prototype.invoke = function (name /*...args*/) { | ||
return this.dispatch("post", [name, array_slice(arguments, 1)]); | ||
}; | ||
/** | ||
@@ -1381,7 +1461,10 @@ * Applies the promised function in a future turn. | ||
*/ | ||
Q.fapply = fapply; | ||
function fapply(value, args) { | ||
return dispatch(value, "apply", [void 0, args]); | ||
} | ||
Q.fapply = function (object, args) { | ||
return Q(object).dispatch("apply", [void 0, args]); | ||
}; | ||
Promise.prototype.fapply = function (args) { | ||
return this.dispatch("apply", [void 0, args]); | ||
}; | ||
/** | ||
@@ -1392,9 +1475,11 @@ * Calls the promised function in a future turn. | ||
*/ | ||
Q["try"] = fcall; // XXX experimental | ||
Q.fcall = fcall; | ||
function fcall(value) { | ||
var args = array_slice(arguments, 1); | ||
return fapply(value, args); | ||
} | ||
Q["try"] = | ||
Q.fcall = function (object /* ...args*/) { | ||
return Q(object).dispatch("apply", [void 0, array_slice(arguments, 1)]); | ||
}; | ||
Promise.prototype.fcall = function (/*...args*/) { | ||
return this.dispatch("apply", [void 0, array_slice(arguments)]); | ||
}; | ||
/** | ||
@@ -1406,10 +1491,22 @@ * Binds the promised function, transforming return values into a fulfilled | ||
*/ | ||
Q.fbind = fbind; | ||
function fbind(value) { | ||
Q.fbind = function (object /*...args*/) { | ||
var promise = Q(object); | ||
var args = array_slice(arguments, 1); | ||
return function fbound() { | ||
var allArgs = args.concat(array_slice(arguments)); | ||
return dispatch(value, "apply", [this, allArgs]); | ||
return promise.dispatch("apply", [ | ||
this, | ||
args.concat(array_slice(arguments)) | ||
]); | ||
}; | ||
} | ||
}; | ||
Promise.prototype.fbind = function (/*...args*/) { | ||
var promise = this; | ||
var args = array_slice(arguments); | ||
return function fbound() { | ||
return promise.dispatch("apply", [ | ||
this, | ||
args.concat(array_slice(arguments)) | ||
]); | ||
}; | ||
}; | ||
@@ -1422,4 +1519,10 @@ /** | ||
*/ | ||
Q.keys = dispatcher("keys"); | ||
Q.keys = function (object) { | ||
return Q(object).dispatch("keys", []); | ||
}; | ||
Promise.prototype.keys = function () { | ||
return this.dispatch("keys", []); | ||
}; | ||
/** | ||
@@ -1448,8 +1551,15 @@ * Turns an array of promises into a promise for an array. If any of | ||
++countDown; | ||
when(promise, function (value) { | ||
promises[index] = value; | ||
if (--countDown === 0) { | ||
deferred.resolve(promises); | ||
when( | ||
promise, | ||
function (value) { | ||
promises[index] = value; | ||
if (--countDown === 0) { | ||
deferred.resolve(promises); | ||
} | ||
}, | ||
deferred.reject, | ||
function (progress) { | ||
deferred.notify({ index: index, value: progress }); | ||
} | ||
}, deferred.reject); | ||
); | ||
} | ||
@@ -1464,2 +1574,6 @@ }, void 0); | ||
Promise.prototype.all = function () { | ||
return all(this); | ||
}; | ||
/** | ||
@@ -1477,3 +1591,3 @@ * Waits for all promises to be settled, either fulfilled or | ||
return when(promises, function (promises) { | ||
promises = array_map(promises, resolve); | ||
promises = array_map(promises, Q); | ||
return when(all(array_map(promises, function (promise) { | ||
@@ -1487,22 +1601,34 @@ return when(promise, noop, noop); | ||
Promise.prototype.allResolved = function () { | ||
return allResolved(this); | ||
}; | ||
/** | ||
* @see Promise#allSettled | ||
*/ | ||
Q.allSettled = allSettled; | ||
function allSettled(values) { | ||
return when(values, function (values) { | ||
return all(array_map(values, function (value, i) { | ||
return when( | ||
value, | ||
function (fulfillmentValue) { | ||
values[i] = { state: "fulfilled", value: fulfillmentValue }; | ||
return values[i]; | ||
}, | ||
function (reason) { | ||
values[i] = { state: "rejected", reason: reason }; | ||
return values[i]; | ||
} | ||
); | ||
})).thenResolve(values); | ||
}); | ||
function allSettled(promises) { | ||
return Q(promises).allSettled(); | ||
} | ||
/** | ||
* Turns an array of promises into a promise for an array of their states (as | ||
* returned by `inspect`) when they have all settled. | ||
* @param {Array[Any*]} values an array (or promise for an array) of values (or | ||
* promises for values) | ||
* @returns {Array[State]} an array of states for the respective values. | ||
*/ | ||
Promise.prototype.allSettled = function () { | ||
return this.then(function (promises) { | ||
return all(array_map(promises, function (promise) { | ||
promise = Q(promise); | ||
function regardless() { | ||
return promise.inspect(); | ||
} | ||
return promise.then(regardless, regardless); | ||
})); | ||
}); | ||
}; | ||
/** | ||
* Captures the failure of a promise, giving an oportunity to recover | ||
@@ -1516,8 +1642,12 @@ * with a callback. If the given promise is fulfilled, the returned | ||
*/ | ||
Q["catch"] = // XXX experimental | ||
Q.fail = fail; | ||
function fail(promise, rejected) { | ||
return when(promise, void 0, rejected); | ||
} | ||
Q.fail = // XXX legacy | ||
Q["catch"] = function (object, rejected) { | ||
return Q(object).then(void 0, rejected); | ||
}; | ||
Promise.prototype.fail = // XXX legacy | ||
Promise.prototype["catch"] = function (rejected) { | ||
return this.then(void 0, rejected); | ||
}; | ||
/** | ||
@@ -1532,6 +1662,10 @@ * Attaches a listener that can respond to progress notifications from a | ||
Q.progress = progress; | ||
function progress(promise, progressed) { | ||
return when(promise, void 0, void 0, progressed); | ||
function progress(object, progressed) { | ||
return Q(object).then(void 0, void 0, progressed); | ||
} | ||
Promise.prototype.progress = function (progressed) { | ||
return this.then(void 0, void 0, progressed); | ||
}; | ||
/** | ||
@@ -1548,15 +1682,21 @@ * Provides an opportunity to observe the settling of a promise, | ||
*/ | ||
Q["finally"] = // XXX experimental | ||
Q.fin = fin; | ||
function fin(promise, callback) { | ||
return when(promise, function (value) { | ||
return when(callback(), function () { | ||
Q.fin = // XXX legacy | ||
Q["finally"] = function (object, callback) { | ||
return Q(object)["finally"](callback); | ||
}; | ||
Promise.prototype.fin = // XXX legacy | ||
Promise.prototype["finally"] = function (callback) { | ||
callback = Q(callback); | ||
return this.then(function (value) { | ||
return callback.fcall().then(function () { | ||
return value; | ||
}); | ||
}, function (exception) { | ||
return when(callback(), function () { | ||
return reject(exception); | ||
}, function (reason) { | ||
// TODO attempt to recycle the rejection with "this". | ||
return callback.fcall().then(function () { | ||
throw reason; | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -1569,4 +1709,7 @@ /** | ||
*/ | ||
Q.done = done; | ||
function done(promise, fulfilled, rejected, progress) { | ||
Q.done = function (object, fulfilled, rejected, progress) { | ||
return Q(object).done(fulfilled, rejected, progress); | ||
}; | ||
Promise.prototype.done = function (fulfilled, rejected, progress) { | ||
var onUnhandledError = function (error) { | ||
@@ -1577,3 +1720,2 @@ // forward to a future turn so that ``when`` | ||
makeStackTraceLong(error, promise); | ||
if (Q.onerror) { | ||
@@ -1588,5 +1730,5 @@ Q.onerror(error); | ||
// Avoid unnecessary `nextTick`ing via an unnecessary `when`. | ||
var promiseToHandle = fulfilled || rejected || progress ? | ||
when(promise, fulfilled, rejected, progress) : | ||
promise; | ||
var promise = fulfilled || rejected || progress ? | ||
this.then(fulfilled, rejected, progress) : | ||
this; | ||
@@ -1596,5 +1738,6 @@ if (typeof process === "object" && process && process.domain) { | ||
} | ||
fail(promiseToHandle, onUnhandledError); | ||
} | ||
promise.then(void 0, onUnhandledError); | ||
}; | ||
/** | ||
@@ -1609,10 +1752,13 @@ * Causes a promise to be rejected if it does not get fulfilled before | ||
*/ | ||
Q.timeout = timeout; | ||
function timeout(promise, ms, msg) { | ||
Q.timeout = function (object, ms, message) { | ||
return Q(object).timeout(ms, message); | ||
}; | ||
Promise.prototype.timeout = function (ms, message) { | ||
var deferred = defer(); | ||
var timeoutId = setTimeout(function () { | ||
deferred.reject(new Error(msg || "Timed out after " + ms + " ms")); | ||
deferred.reject(new Error(message || "Timed out after " + ms + " ms")); | ||
}, ms); | ||
when(promise, function (value) { | ||
this.then(function (value) { | ||
clearTimeout(timeoutId); | ||
@@ -1626,29 +1772,31 @@ deferred.resolve(value); | ||
return deferred.promise; | ||
} | ||
}; | ||
/** | ||
* Returns a promise for the given value (or promised value) after some | ||
* milliseconds. | ||
* Returns a promise for the given value (or promised value), some | ||
* milliseconds after it resolved. Passes rejections immediately. | ||
* @param {Any*} promise | ||
* @param {Number} milliseconds | ||
* @returns a promise for the resolution of the given promise after some | ||
* time has elapsed. | ||
* @returns a promise for the resolution of the given promise after milliseconds | ||
* time has elapsed since the resolution of the given promise. | ||
* If the given promise rejects, that is passed immediately. | ||
*/ | ||
Q.delay = delay; | ||
function delay(promise, timeout) { | ||
Q.delay = function (object, timeout) { | ||
if (timeout === void 0) { | ||
timeout = promise; | ||
promise = void 0; | ||
timeout = object; | ||
object = void 0; | ||
} | ||
return Q(object).delay(timeout); | ||
}; | ||
var deferred = defer(); | ||
Promise.prototype.delay = function (timeout) { | ||
return this.then(function (value) { | ||
var deferred = defer(); | ||
setTimeout(function () { | ||
deferred.resolve(value); | ||
}, timeout); | ||
return deferred.promise; | ||
}); | ||
}; | ||
when(promise, undefined, undefined, deferred.notify); | ||
setTimeout(function () { | ||
deferred.resolve(promise); | ||
}, timeout); | ||
return deferred.promise; | ||
} | ||
/** | ||
@@ -1663,11 +1811,13 @@ * Passes a continuation to a Node function, which is called with the given | ||
*/ | ||
Q.nfapply = nfapply; | ||
function nfapply(callback, args) { | ||
Q.nfapply = function (callback, args) { | ||
return Q(callback).nfapply(args); | ||
}; | ||
Promise.prototype.nfapply = function (args) { | ||
var deferred = defer(); | ||
var nodeArgs = array_slice(args); | ||
var deferred = defer(); | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
fapply(callback, nodeArgs).fail(deferred.reject); | ||
this.fapply(nodeArgs).fail(deferred.reject); | ||
return deferred.promise; | ||
} | ||
}; | ||
@@ -1677,17 +1827,20 @@ /** | ||
* arguments provided individually, and returns a promise. | ||
* @example | ||
* Q.nfcall(FS.readFile, __filename) | ||
* .then(function (content) { | ||
* }) | ||
* | ||
* Q.nfcall(FS.readFile, __filename) | ||
* .then(function (content) { | ||
* }) | ||
* | ||
*/ | ||
Q.nfcall = nfcall; | ||
function nfcall(callback/*, ...args */) { | ||
var nodeArgs = array_slice(arguments, 1); | ||
Q.nfcall = function (callback /*...args*/) { | ||
var args = array_slice(arguments, 1); | ||
return Q(callback).nfapply(args); | ||
}; | ||
Promise.prototype.nfcall = function (/*...args*/) { | ||
var nodeArgs = array_slice(arguments); | ||
var deferred = defer(); | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
fapply(callback, nodeArgs).fail(deferred.reject); | ||
this.fapply(nodeArgs).fail(deferred.reject); | ||
return deferred.promise; | ||
} | ||
}; | ||
@@ -1697,11 +1850,9 @@ /** | ||
* version that returns a promise. | ||
* | ||
* Q.nfbind(FS.readFile, __filename)("utf-8") | ||
* .then(console.log) | ||
* .done() | ||
* | ||
* @example | ||
* Q.nfbind(FS.readFile, __filename)("utf-8") | ||
* .then(console.log) | ||
* .done() | ||
*/ | ||
Q.nfbind = nfbind; | ||
Q.denodeify = Q.nfbind; // synonyms | ||
function nfbind(callback/*, ...args */) { | ||
Q.nfbind = | ||
Q.denodeify = function (callback /*...args*/) { | ||
var baseArgs = array_slice(arguments, 1); | ||
@@ -1712,10 +1863,15 @@ return function () { | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
fapply(callback, nodeArgs).fail(deferred.reject); | ||
Q(callback).fapply(nodeArgs).fail(deferred.reject); | ||
return deferred.promise; | ||
}; | ||
} | ||
}; | ||
Q.nbind = nbind; | ||
function nbind(callback, thisArg /*, ... args*/) { | ||
Promise.prototype.nfbind = | ||
Promise.prototype.denodeify = function (/*...args*/) { | ||
var args = array_slice(arguments); | ||
args.unshift(this); | ||
return Q.denodeify.apply(void 0, args); | ||
}; | ||
Q.nbind = function (callback, thisp /*...args*/) { | ||
var baseArgs = array_slice(arguments, 2); | ||
@@ -1726,12 +1882,16 @@ return function () { | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
function bound() { | ||
return callback.apply(thisArg, arguments); | ||
return callback.apply(thisp, arguments); | ||
} | ||
fapply(bound, nodeArgs).fail(deferred.reject); | ||
Q(bound).fapply(nodeArgs).fail(deferred.reject); | ||
return deferred.promise; | ||
}; | ||
} | ||
}; | ||
Promise.prototype.nbind = function (/*thisp, ...args*/) { | ||
var args = array_slice(arguments, 0); | ||
args.unshift(this); | ||
return Q.nbind.apply(void 0, args); | ||
}; | ||
/** | ||
@@ -1746,12 +1906,15 @@ * Calls a method of a Node-style object that accepts a Node-style | ||
*/ | ||
Q.npost = npost; | ||
Q.nmapply = npost; // synonyms | ||
function npost(object, name, args) { | ||
Q.nmapply = // XXX As proposed by "Redsandro" | ||
Q.npost = function (object, name, args) { | ||
return Q(object).npost(name, args); | ||
}; | ||
Promise.prototype.nmapply = // XXX As proposed by "Redsandro" | ||
Promise.prototype.npost = function (name, args) { | ||
var nodeArgs = array_slice(args || []); | ||
var deferred = defer(); | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
post(object, name, nodeArgs).fail(deferred.reject); | ||
this.dispatch("post", [name, nodeArgs]).fail(deferred.reject); | ||
return deferred.promise; | ||
} | ||
}; | ||
@@ -1768,17 +1931,40 @@ /** | ||
*/ | ||
Q.nsend = nsend; | ||
Q.ninvoke = Q.nsend; // synonyms | ||
Q.nmcall = Q.nsend; // synonyms | ||
function nsend(object, name /*, ...args*/) { | ||
Q.nsend = // XXX Based on Mark Miller's proposed "send" | ||
Q.nmcall = // XXX Based on "Redsandro's" proposal | ||
Q.ninvoke = function (object, name /*...args*/) { | ||
var nodeArgs = array_slice(arguments, 2); | ||
var deferred = defer(); | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
post(object, name, nodeArgs).fail(deferred.reject); | ||
Q(object).dispatch("post", [name, nodeArgs]).fail(deferred.reject); | ||
return deferred.promise; | ||
}; | ||
Promise.prototype.nsend = // XXX Based on Mark Miller's proposed "send" | ||
Promise.prototype.nmcall = // XXX Based on "Redsandro's" proposal | ||
Promise.prototype.ninvoke = function (name /*...args*/) { | ||
var nodeArgs = array_slice(arguments, 1); | ||
var deferred = defer(); | ||
nodeArgs.push(deferred.makeNodeResolver()); | ||
this.dispatch("post", [name, nodeArgs]).fail(deferred.reject); | ||
return deferred.promise; | ||
}; | ||
/** | ||
* If a function would like to support both Node continuation-passing-style and | ||
* promise-returning-style, it can end its internal promise chain with | ||
* `nodeify(nodeback)`, forwarding the optional nodeback argument. If the user | ||
* elects to use a nodeback, the result will be sent there. If they do not | ||
* pass a nodeback, they will receive the result promise. | ||
* @param object a result (or a promise for a result) | ||
* @param {Function} nodeback a Node.js-style callback | ||
* @returns either the promise or nothing | ||
*/ | ||
Q.nodeify = nodeify; | ||
function nodeify(object, nodeback) { | ||
return Q(object).nodeify(nodeback); | ||
} | ||
Q.nodeify = nodeify; | ||
function nodeify(promise, nodeback) { | ||
Promise.prototype.nodeify = function (nodeback) { | ||
if (nodeback) { | ||
promise.then(function (value) { | ||
this.then(function (value) { | ||
nextTick(function () { | ||
@@ -1793,5 +1979,5 @@ nodeback(null, value); | ||
} else { | ||
return promise; | ||
return this; | ||
} | ||
} | ||
}; | ||
@@ -1798,0 +1984,0 @@ // All code before this point will be filtered from stack traces. |
Sorry, the diff of this file is not supported yet
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
242003
26
3778
1