Ignore:
Timestamp:
03/08/14 11:41:10 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Update node modules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/node_modules/q/q.js

    r484 r489  
    9292
    9393    function flush() {
     94        /* jshint loopfunc: true */
     95
    9496        while (head.next) {
    9597            head = head.next;
     
    114116                    // listening "uncaughtException" events (as domains does).
    115117                    // Continue in next event to avoid tick recursion.
    116                     domain && domain.exit();
     118                    if (domain) {
     119                        domain.exit();
     120                    }
    117121                    setTimeout(flush, 0);
    118                     domain && domain.enter();
     122                    if (domain) {
     123                        domain.enter();
     124                    }
    119125
    120126                    throw e;
     
    173179        // http://www.nonblocking.io/2011/06/windownexttick.html
    174180        var channel = new MessageChannel();
    175         channel.port1.onmessage = flush;
     181        // At least Safari Version 6.0.5 (8536.30.1) intermittently cannot create
     182        // working message ports the first time a page loads.
     183        channel.port1.onmessage = function () {
     184            requestTick = requestPortTick;
     185            channel.port1.onmessage = flush;
     186            flush();
     187        };
     188        var requestPortTick = function () {
     189            // Opera requires us to provide a message payload, regardless of
     190            // whether we use it.
     191            channel.port2.postMessage(0);
     192        };
    176193        requestTick = function () {
    177             channel.port2.postMessage(0);
     194            setTimeout(flush, 0);
     195            requestPortTick();
    178196        };
    179197
     
    199217// See Mark Miller’s explanation of what this does.
    200218// http://wiki.ecmascript.org/doku.php?id=conventions:safe_meta_programming
     219var call = Function.call;
    201220function uncurryThis(f) {
    202     var call = Function.call;
    203221    return function () {
    204222        return call.apply(f, arguments);
     
    442460
    443461/**
    444  * Creates fulfilled promises from non-thenables,
    445  * Passes Q promises through,
    446  * Coerces other thenables to Q promises.
     462 * Constructs a promise for an immediate reference, passes promises through, or
     463 * coerces promises from different systems.
     464 * @param value immediate reference or promise
    447465 */
    448466function Q(value) {
    449     return resolve(value);
    450 }
     467    // If the object is already a Promise, return it directly.  This enables
     468    // the resolve function to both be used to created references from objects,
     469    // but to tolerably coerce non-promises to promises.
     470    if (isPromise(value)) {
     471        return value;
     472    }
     473
     474    // assimilate thenables
     475    if (isPromiseAlike(value)) {
     476        return coerce(value);
     477    } else {
     478        return fulfill(value);
     479    }
     480}
     481Q.resolve = Q;
    451482
    452483/**
     
    555586        }
    556587
    557         become(resolve(value));
     588        become(Q(value));
    558589    };
    559590
     
    616647        throw new TypeError("resolver must be a function.");
    617648    }
    618 
    619649    var deferred = defer();
    620     fcall(
    621         resolver,
    622         deferred.resolve,
    623         deferred.reject,
    624         deferred.notify
    625     ).fail(deferred.reject);
     650    try {
     651        resolver(deferred.resolve, deferred.reject, deferred.notify);
     652    } catch (reason) {
     653        deferred.reject(reason);
     654    }
    626655    return deferred.promise;
    627656}
     657
     658// XXX experimental.  This method is a way to denote that a local value is
     659// serializable and should be immediately dispatched to a remote upon request,
     660// instead of passing a reference.
     661Q.passByCopy = function (object) {
     662    //freeze(object);
     663    //passByCopies.set(object, true);
     664    return object;
     665};
     666
     667Promise.prototype.passByCopy = function () {
     668    //freeze(object);
     669    //passByCopies.set(object, true);
     670    return this;
     671};
     672
     673/**
     674 * If two promises eventually fulfill to the same value, promises that value,
     675 * but otherwise rejects.
     676 * @param x {Any*}
     677 * @param y {Any*}
     678 * @returns {Any*} a promise for x and y if they are the same, but a rejection
     679 * otherwise.
     680 *
     681 */
     682Q.join = function (x, y) {
     683    return Q(x).join(y);
     684};
     685
     686Promise.prototype.join = function (that) {
     687    return Q([this, that]).spread(function (x, y) {
     688        if (x === y) {
     689            // TODO: "===" should be Object.is or equiv
     690            return x;
     691        } else {
     692            throw new Error("Can't join: not the same: " + x + " " + y);
     693        }
     694    });
     695};
     696
     697/**
     698 * Returns a promise for the first of an array of promises to become fulfilled.
     699 * @param answers {Array[Any*]} promises to race
     700 * @returns {Any*} the first promise to be fulfilled
     701 */
     702Q.race = race;
     703function race(answerPs) {
     704    return promise(function(resolve, reject) {
     705        // Switch to this once we can assume at least ES5
     706        // answerPs.forEach(function(answerP) {
     707        //     Q(answerP).then(resolve, reject);
     708        // });
     709        // Use this in the meantime
     710        for (var i = 0, len = answerPs.length; i < len; i++) {
     711            Q(answerPs[i]).then(resolve, reject);
     712        }
     713    });
     714}
     715
     716Promise.prototype.race = function () {
     717    return this.then(Q.race);
     718};
    628719
    629720/**
     
    692783    return promise;
    693784}
     785
     786Promise.prototype.toString = function () {
     787    return "[object Promise]";
     788};
    694789
    695790Promise.prototype.then = function (fulfilled, rejected, progressed) {
     
    764859};
    765860
     861/**
     862 * Registers an observer on a promise.
     863 *
     864 * Guarantees:
     865 *
     866 * 1. that fulfilled and rejected will be called only once.
     867 * 2. that either the fulfilled callback or the rejected callback will be
     868 *    called, but not both.
     869 * 3. that fulfilled and rejected will not be called in this turn.
     870 *
     871 * @param value      promise or immediate reference to observe
     872 * @param fulfilled  function to be called with the fulfilled value
     873 * @param rejected   function to be called with the rejection exception
     874 * @param progressed function to be called on any progress notifications
     875 * @return promise for the return value from the invoked callback
     876 */
     877Q.when = when;
     878function when(value, fulfilled, rejected, progressed) {
     879    return Q(value).then(fulfilled, rejected, progressed);
     880}
     881
    766882Promise.prototype.thenResolve = function (value) {
    767     return when(this, function () { return value; });
     883    return this.then(function () { return value; });
     884};
     885
     886Q.thenResolve = function (promise, value) {
     887    return Q(promise).thenResolve(value);
    768888};
    769889
    770890Promise.prototype.thenReject = function (reason) {
    771     return when(this, function () { throw reason; });
    772 };
    773 
    774 // Chainable methods
    775 array_reduce(
    776     [
    777         "isFulfilled", "isRejected", "isPending",
    778         "dispatch",
    779         "when", "spread",
    780         "get", "set", "del", "delete",
    781         "post", "send", "mapply", "invoke", "mcall",
    782         "keys",
    783         "fapply", "fcall", "fbind",
    784         "all", "allResolved",
    785         "timeout", "delay",
    786         "catch", "finally", "fail", "fin", "progress", "done",
    787         "nfcall", "nfapply", "nfbind", "denodeify", "nbind",
    788         "npost", "nsend", "nmapply", "ninvoke", "nmcall",
    789         "nodeify"
    790     ],
    791     function (undefined, name) {
    792         Promise.prototype[name] = function () {
    793             return Q[name].apply(
    794                 Q,
    795                 [this].concat(array_slice(arguments))
    796             );
    797         };
    798     },
    799     void 0
    800 );
    801 
    802 Promise.prototype.toSource = function () {
    803     return this.toString();
    804 };
    805 
    806 Promise.prototype.toString = function () {
    807     return "[object Promise]";
     891    return this.then(function () { throw reason; });
     892};
     893
     894Q.thenReject = function (promise, reason) {
     895    return Q(promise).thenReject(reason);
    808896};
    809897
     
    855943}
    856944
     945Promise.prototype.isPending = function () {
     946    return this.inspect().state === "pending";
     947};
     948
    857949/**
    858950 * @returns whether the given object is a value or fulfilled
     
    864956}
    865957
     958Promise.prototype.isFulfilled = function () {
     959    return this.inspect().state === "fulfilled";
     960};
     961
    866962/**
    867963 * @returns whether the given object is a rejected promise.
     
    871967    return isPromise(object) && object.inspect().state === "rejected";
    872968}
     969
     970Promise.prototype.isRejected = function () {
     971    return this.inspect().state === "rejected";
     972};
    873973
    874974//// BEGIN UNHANDLED REJECTION TRACKING
     
    899999    for (var i = 0; i < unhandledReasons.length; i++) {
    9001000        var reason = unhandledReasons[i];
    901         if (reason && typeof reason.stack !== "undefined") {
    902             console.warn("Unhandled rejection reason:", reason.stack);
    903         } else {
    904             console.warn("Unhandled rejection reason (no stack):", reason);
    905         }
     1001        console.warn("Unhandled rejection reason:", reason);
    9061002    }
    9071003}
     
    9301026
    9311027    unhandledRejections.push(promise);
    932     unhandledReasons.push(reason);
     1028    if (reason && typeof reason.stack !== "undefined") {
     1029        unhandledReasons.push(reason.stack);
     1030    } else {
     1031        unhandledReasons.push("(no stack) " + reason);
     1032    }
    9331033    displayUnhandledReasons();
    9341034}
     
    10191119            }
    10201120        },
    1021         "apply": function (thisP, args) {
    1022             return value.apply(thisP, args);
     1121        "apply": function (thisp, args) {
     1122            return value.apply(thisp, args);
    10231123        },
    10241124        "keys": function () {
     
    10281128        return { state: "fulfilled", value: value };
    10291129    });
    1030 }
    1031 
    1032 /**
    1033  * Constructs a promise for an immediate reference, passes promises through, or
    1034  * coerces promises from different systems.
    1035  * @param value immediate reference or promise
    1036  */
    1037 Q.resolve = resolve;
    1038 function resolve(value) {
    1039     // If the object is already a Promise, return it directly.  This enables
    1040     // the resolve function to both be used to created references from objects,
    1041     // but to tolerably coerce non-promises to promises.
    1042     if (isPromise(value)) {
    1043         return value;
    1044     }
    1045 
    1046     // assimilate thenables
    1047     if (isPromiseAlike(value)) {
    1048         return coerce(value);
    1049     } else {
    1050         return fulfill(value);
    1051     }
    10521130}
    10531131
     
    10851163        return dispatch(object, op, args);
    10861164    }, function () {
    1087         return resolve(object).inspect();
     1165        return Q(object).inspect();
    10881166    });
    1089 }
    1090 
    1091 /**
    1092  * Registers an observer on a promise.
    1093  *
    1094  * Guarantees:
    1095  *
    1096  * 1. that fulfilled and rejected will be called only once.
    1097  * 2. that either the fulfilled callback or the rejected callback will be
    1098  *    called, but not both.
    1099  * 3. that fulfilled and rejected will not be called in this turn.
    1100  *
    1101  * @param value      promise or immediate reference to observe
    1102  * @param fulfilled  function to be called with the fulfilled value
    1103  * @param rejected   function to be called with the rejection exception
    1104  * @param progressed function to be called on any progress notifications
    1105  * @return promise for the return value from the invoked callback
    1106  */
    1107 Q.when = when;
    1108 function when(value, fulfilled, rejected, progressed) {
    1109     return Q(value).then(fulfilled, rejected, progressed);
    11101167}
    11111168
     
    11211178 */
    11221179Q.spread = spread;
    1123 function spread(promise, fulfilled, rejected) {
    1124     return when(promise, function (valuesOrPromises) {
    1125         return all(valuesOrPromises).then(function (values) {
    1126             return fulfilled.apply(void 0, values);
    1127         }, rejected);
     1180function spread(value, fulfilled, rejected) {
     1181    return Q(value).spread(fulfilled, rejected);
     1182}
     1183
     1184Promise.prototype.spread = function (fulfilled, rejected) {
     1185    return this.all().then(function (array) {
     1186        return fulfilled.apply(void 0, array);
    11281187    }, rejected);
    1129 }
     1188};
    11301189
    11311190/**
     
    11881247        }
    11891248        var generator = makeGenerator.apply(this, arguments);
    1190         var callback = continuer.bind(continuer, "send");
     1249        var callback = continuer.bind(continuer, "next");
    11911250        var errback = continuer.bind(continuer, "throw");
    11921251        return callback();
     
    12461305 *     return a + b;
    12471306 * });
    1248  * add(Q.resolve(a), Q.resolve(B));
     1307 * add(Q(a), Q(B));
    12491308 *
    12501309 * @param {function} callback The function to decorate
     
    12691328Q.dispatch = dispatch;
    12701329function dispatch(object, op, args) {
     1330    return Q(object).dispatch(op, args);
     1331}
     1332
     1333Promise.prototype.dispatch = function (op, args) {
     1334    var self = this;
    12711335    var deferred = defer();
    12721336    nextTick(function () {
    1273         resolve(object).promiseDispatch(deferred.resolve, op, args);
     1337        self.promiseDispatch(deferred.resolve, op, args);
    12741338    });
    12751339    return deferred.promise;
    1276 }
    1277 
    1278 /**
    1279  * Constructs a promise method that can be used to safely observe resolution of
    1280  * a promise for an arbitrarily named method like "propfind" in a future turn.
    1281  *
    1282  * "dispatcher" constructs methods like "get(promise, name)" and "set(promise)".
    1283  */
    1284 Q.dispatcher = dispatcher;
    1285 function dispatcher(op) {
    1286     return function (object) {
    1287         var args = array_slice(arguments, 1);
    1288         return dispatch(object, op, args);
    1289     };
    1290 }
     1340};
    12911341
    12921342/**
     
    12961346 * @return promise for the property value
    12971347 */
    1298 Q.get = dispatcher("get");
     1348Q.get = function (object, key) {
     1349    return Q(object).dispatch("get", [key]);
     1350};
     1351
     1352Promise.prototype.get = function (key) {
     1353    return this.dispatch("get", [key]);
     1354};
    12991355
    13001356/**
     
    13051361 * @return promise for the return value
    13061362 */
    1307 Q.set = dispatcher("set");
     1363Q.set = function (object, key, value) {
     1364    return Q(object).dispatch("set", [key, value]);
     1365};
     1366
     1367Promise.prototype.set = function (key, value) {
     1368    return this.dispatch("set", [key, value]);
     1369};
    13081370
    13091371/**
     
    13131375 * @return promise for the return value
    13141376 */
    1315 Q["delete"] = // XXX experimental
    1316 Q.del = dispatcher("delete");
     1377Q.del = // XXX legacy
     1378Q["delete"] = function (object, key) {
     1379    return Q(object).dispatch("delete", [key]);
     1380};
     1381
     1382Promise.prototype.del = // XXX legacy
     1383Promise.prototype["delete"] = function (key) {
     1384    return this.dispatch("delete", [key]);
     1385};
    13171386
    13181387/**
     
    13291398 */
    13301399// bound locally because it is used by other methods
    1331 var post = Q.post = dispatcher("post");
    1332 Q.mapply = post; // experimental
     1400Q.mapply = // XXX As proposed by "Redsandro"
     1401Q.post = function (object, name, args) {
     1402    return Q(object).dispatch("post", [name, args]);
     1403};
     1404
     1405Promise.prototype.mapply = // XXX As proposed by "Redsandro"
     1406Promise.prototype.post = function (name, args) {
     1407    return this.dispatch("post", [name, args]);
     1408};
    13331409
    13341410/**
     
    13391415 * @return promise for the return value
    13401416 */
    1341 Q.send = send;
    1342 Q.invoke = send; // synonyms
    1343 Q.mcall = send; // experimental
    1344 function send(value, name) {
    1345     var args = array_slice(arguments, 2);
    1346     return post(value, name, args);
    1347 }
     1417Q.send = // XXX Mark Miller's proposed parlance
     1418Q.mcall = // XXX As proposed by "Redsandro"
     1419Q.invoke = function (object, name /*...args*/) {
     1420    return Q(object).dispatch("post", [name, array_slice(arguments, 2)]);
     1421};
     1422
     1423Promise.prototype.send = // XXX Mark Miller's proposed parlance
     1424Promise.prototype.mcall = // XXX As proposed by "Redsandro"
     1425Promise.prototype.invoke = function (name /*...args*/) {
     1426    return this.dispatch("post", [name, array_slice(arguments, 1)]);
     1427};
    13481428
    13491429/**
     
    13521432 * @param args      array of application arguments
    13531433 */
    1354 Q.fapply = fapply;
    1355 function fapply(value, args) {
    1356     return dispatch(value, "apply", [void 0, args]);
    1357 }
     1434Q.fapply = function (object, args) {
     1435    return Q(object).dispatch("apply", [void 0, args]);
     1436};
     1437
     1438Promise.prototype.fapply = function (args) {
     1439    return this.dispatch("apply", [void 0, args]);
     1440};
    13581441
    13591442/**
     
    13621445 * @param ...args   array of application arguments
    13631446 */
    1364 Q["try"] = fcall; // XXX experimental
    1365 Q.fcall = fcall;
    1366 function fcall(value) {
    1367     var args = array_slice(arguments, 1);
    1368     return fapply(value, args);
    1369 }
     1447Q["try"] =
     1448Q.fcall = function (object /* ...args*/) {
     1449    return Q(object).dispatch("apply", [void 0, array_slice(arguments, 1)]);
     1450};
     1451
     1452Promise.prototype.fcall = function (/*...args*/) {
     1453    return this.dispatch("apply", [void 0, array_slice(arguments)]);
     1454};
    13701455
    13711456/**
     
    13751460 * @param ...args   array of application arguments
    13761461 */
    1377 Q.fbind = fbind;
    1378 function fbind(value) {
     1462Q.fbind = function (object /*...args*/) {
     1463    var promise = Q(object);
    13791464    var args = array_slice(arguments, 1);
    13801465    return function fbound() {
    1381         var allArgs = args.concat(array_slice(arguments));
    1382         return dispatch(value, "apply", [this, allArgs]);
     1466        return promise.dispatch("apply", [
     1467            this,
     1468            args.concat(array_slice(arguments))
     1469        ]);
    13831470    };
    1384 }
     1471};
     1472Promise.prototype.fbind = function (/*...args*/) {
     1473    var promise = this;
     1474    var args = array_slice(arguments);
     1475    return function fbound() {
     1476        return promise.dispatch("apply", [
     1477            this,
     1478            args.concat(array_slice(arguments))
     1479        ]);
     1480    };
     1481};
    13851482
    13861483/**
     
    13901487 * @return promise for the keys of the eventually settled object
    13911488 */
    1392 Q.keys = dispatcher("keys");
     1489Q.keys = function (object) {
     1490    return Q(object).dispatch("keys", []);
     1491};
     1492
     1493Promise.prototype.keys = function () {
     1494    return this.dispatch("keys", []);
     1495};
    13931496
    13941497/**
     
    14151518            } else {
    14161519                ++countDown;
    1417                 when(promise, function (value) {
    1418                     promises[index] = value;
    1419                     if (--countDown === 0) {
    1420                         deferred.resolve(promises);
     1520                when(
     1521                    promise,
     1522                    function (value) {
     1523                        promises[index] = value;
     1524                        if (--countDown === 0) {
     1525                            deferred.resolve(promises);
     1526                        }
     1527                    },
     1528                    deferred.reject,
     1529                    function (progress) {
     1530                        deferred.notify({ index: index, value: progress });
    14211531                    }
    1422                 }, deferred.reject);
     1532                );
    14231533            }
    14241534        }, void 0);
     
    14291539    });
    14301540}
     1541
     1542Promise.prototype.all = function () {
     1543    return all(this);
     1544};
    14311545
    14321546/**
     
    14421556function allResolved(promises) {
    14431557    return when(promises, function (promises) {
    1444         promises = array_map(promises, resolve);
     1558        promises = array_map(promises, Q);
    14451559        return when(all(array_map(promises, function (promise) {
    14461560            return when(promise, noop, noop);
     
    14511565}
    14521566
     1567Promise.prototype.allResolved = function () {
     1568    return allResolved(this);
     1569};
     1570
     1571/**
     1572 * @see Promise#allSettled
     1573 */
    14531574Q.allSettled = allSettled;
    1454 function allSettled(values) {
    1455     return when(values, function (values) {
    1456         return all(array_map(values, function (value, i) {
    1457             return when(
    1458                 value,
    1459                 function (fulfillmentValue) {
    1460                     values[i] = { state: "fulfilled", value: fulfillmentValue };
    1461                     return values[i];
    1462                 },
    1463                 function (reason) {
    1464                     values[i] = { state: "rejected", reason: reason };
    1465                     return values[i];
    1466                 }
    1467             );
    1468         })).thenResolve(values);
     1575function allSettled(promises) {
     1576    return Q(promises).allSettled();
     1577}
     1578
     1579/**
     1580 * Turns an array of promises into a promise for an array of their states (as
     1581 * returned by `inspect`) when they have all settled.
     1582 * @param {Array[Any*]} values an array (or promise for an array) of values (or
     1583 * promises for values)
     1584 * @returns {Array[State]} an array of states for the respective values.
     1585 */
     1586Promise.prototype.allSettled = function () {
     1587    return this.then(function (promises) {
     1588        return all(array_map(promises, function (promise) {
     1589            promise = Q(promise);
     1590            function regardless() {
     1591                return promise.inspect();
     1592            }
     1593            return promise.then(regardless, regardless);
     1594        }));
    14691595    });
    1470 }
     1596};
    14711597
    14721598/**
     
    14791605 * @returns a promise for the return value of the callback
    14801606 */
    1481 Q["catch"] = // XXX experimental
    1482 Q.fail = fail;
    1483 function fail(promise, rejected) {
    1484     return when(promise, void 0, rejected);
    1485 }
     1607Q.fail = // XXX legacy
     1608Q["catch"] = function (object, rejected) {
     1609    return Q(object).then(void 0, rejected);
     1610};
     1611
     1612Promise.prototype.fail = // XXX legacy
     1613Promise.prototype["catch"] = function (rejected) {
     1614    return this.then(void 0, rejected);
     1615};
    14861616
    14871617/**
     
    14941624 */
    14951625Q.progress = progress;
    1496 function progress(promise, progressed) {
    1497     return when(promise, void 0, void 0, progressed);
    1498 }
     1626function progress(object, progressed) {
     1627    return Q(object).then(void 0, void 0, progressed);
     1628}
     1629
     1630Promise.prototype.progress = function (progressed) {
     1631    return this.then(void 0, void 0, progressed);
     1632};
    14991633
    15001634/**
     
    15091643 * ``fin`` is done.
    15101644 */
    1511 Q["finally"] = // XXX experimental
    1512 Q.fin = fin;
    1513 function fin(promise, callback) {
    1514     return when(promise, function (value) {
    1515         return when(callback(), function () {
     1645Q.fin = // XXX legacy
     1646Q["finally"] = function (object, callback) {
     1647    return Q(object)["finally"](callback);
     1648};
     1649
     1650Promise.prototype.fin = // XXX legacy
     1651Promise.prototype["finally"] = function (callback) {
     1652    callback = Q(callback);
     1653    return this.then(function (value) {
     1654        return callback.fcall().then(function () {
    15161655            return value;
    15171656        });
    1518     }, function (exception) {
    1519         return when(callback(), function () {
    1520             return reject(exception);
     1657    }, function (reason) {
     1658        // TODO attempt to recycle the rejection with "this".
     1659        return callback.fcall().then(function () {
     1660            throw reason;
    15211661        });
    15221662    });
    1523 }
     1663};
    15241664
    15251665/**
     
    15291669 * @returns nothing
    15301670 */
    1531 Q.done = done;
    1532 function done(promise, fulfilled, rejected, progress) {
     1671Q.done = function (object, fulfilled, rejected, progress) {
     1672    return Q(object).done(fulfilled, rejected, progress);
     1673};
     1674
     1675Promise.prototype.done = function (fulfilled, rejected, progress) {
    15331676    var onUnhandledError = function (error) {
    15341677        // forward to a future turn so that ``when``
     
    15361679        nextTick(function () {
    15371680            makeStackTraceLong(error, promise);
    1538 
    15391681            if (Q.onerror) {
    15401682                Q.onerror(error);
     
    15461688
    15471689    // Avoid unnecessary `nextTick`ing via an unnecessary `when`.
    1548     var promiseToHandle = fulfilled || rejected || progress ?
    1549         when(promise, fulfilled, rejected, progress) :
    1550         promise;
     1690    var promise = fulfilled || rejected || progress ?
     1691        this.then(fulfilled, rejected, progress) :
     1692        this;
    15511693
    15521694    if (typeof process === "object" && process && process.domain) {
    15531695        onUnhandledError = process.domain.bind(onUnhandledError);
    15541696    }
    1555     fail(promiseToHandle, onUnhandledError);
    1556 }
     1697
     1698    promise.then(void 0, onUnhandledError);
     1699};
    15571700
    15581701/**
     
    15651708 * fulfilled before the timeout, otherwise rejected.
    15661709 */
    1567 Q.timeout = timeout;
    1568 function timeout(promise, ms, msg) {
     1710Q.timeout = function (object, ms, message) {
     1711    return Q(object).timeout(ms, message);
     1712};
     1713
     1714Promise.prototype.timeout = function (ms, message) {
    15691715    var deferred = defer();
    15701716    var timeoutId = setTimeout(function () {
    1571         deferred.reject(new Error(msg || "Timed out after " + ms + " ms"));
     1717        deferred.reject(new Error(message || "Timed out after " + ms + " ms"));
    15721718    }, ms);
    15731719
    1574     when(promise, function (value) {
     1720    this.then(function (value) {
    15751721        clearTimeout(timeoutId);
    15761722        deferred.resolve(value);
     
    15811727
    15821728    return deferred.promise;
    1583 }
    1584 
    1585 /**
    1586  * Returns a promise for the given value (or promised value) after some
    1587  * milliseconds.
     1729};
     1730
     1731/**
     1732 * Returns a promise for the given value (or promised value), some
     1733 * milliseconds after it resolved. Passes rejections immediately.
    15881734 * @param {Any*} promise
    15891735 * @param {Number} milliseconds
    1590  * @returns a promise for the resolution of the given promise after some
    1591  * time has elapsed.
    1592  */
    1593 Q.delay = delay;
    1594 function delay(promise, timeout) {
     1736 * @returns a promise for the resolution of the given promise after milliseconds
     1737 * time has elapsed since the resolution of the given promise.
     1738 * If the given promise rejects, that is passed immediately.
     1739 */
     1740Q.delay = function (object, timeout) {
    15951741    if (timeout === void 0) {
    1596         timeout = promise;
    1597         promise = void 0;
    1598     }
    1599 
    1600     var deferred = defer();
    1601 
    1602     when(promise, undefined, undefined, deferred.notify);
    1603     setTimeout(function () {
    1604         deferred.resolve(promise);
    1605     }, timeout);
    1606 
    1607     return deferred.promise;
    1608 }
     1742        timeout = object;
     1743        object = void 0;
     1744    }
     1745    return Q(object).delay(timeout);
     1746};
     1747
     1748Promise.prototype.delay = function (timeout) {
     1749    return this.then(function (value) {
     1750        var deferred = defer();
     1751        setTimeout(function () {
     1752            deferred.resolve(value);
     1753        }, timeout);
     1754        return deferred.promise;
     1755    });
     1756};
    16091757
    16101758/**
     
    16171765 *
    16181766 */
    1619 Q.nfapply = nfapply;
    1620 function nfapply(callback, args) {
     1767Q.nfapply = function (callback, args) {
     1768    return Q(callback).nfapply(args);
     1769};
     1770
     1771Promise.prototype.nfapply = function (args) {
     1772    var deferred = defer();
    16211773    var nodeArgs = array_slice(args);
     1774    nodeArgs.push(deferred.makeNodeResolver());
     1775    this.fapply(nodeArgs).fail(deferred.reject);
     1776    return deferred.promise;
     1777};
     1778
     1779/**
     1780 * Passes a continuation to a Node function, which is called with the given
     1781 * arguments provided individually, and returns a promise.
     1782 * @example
     1783 * Q.nfcall(FS.readFile, __filename)
     1784 * .then(function (content) {
     1785 * })
     1786 *
     1787 */
     1788Q.nfcall = function (callback /*...args*/) {
     1789    var args = array_slice(arguments, 1);
     1790    return Q(callback).nfapply(args);
     1791};
     1792
     1793Promise.prototype.nfcall = function (/*...args*/) {
     1794    var nodeArgs = array_slice(arguments);
    16221795    var deferred = defer();
    16231796    nodeArgs.push(deferred.makeNodeResolver());
    1624 
    1625     fapply(callback, nodeArgs).fail(deferred.reject);
     1797    this.fapply(nodeArgs).fail(deferred.reject);
    16261798    return deferred.promise;
    1627 }
    1628 
    1629 /**
    1630  * Passes a continuation to a Node function, which is called with the given
    1631  * arguments provided individually, and returns a promise.
    1632  *
    1633  *      Q.nfcall(FS.readFile, __filename)
    1634  *      .then(function (content) {
    1635  *      })
    1636  *
    1637  */
    1638 Q.nfcall = nfcall;
    1639 function nfcall(callback/*, ...args */) {
    1640     var nodeArgs = array_slice(arguments, 1);
    1641     var deferred = defer();
    1642     nodeArgs.push(deferred.makeNodeResolver());
    1643 
    1644     fapply(callback, nodeArgs).fail(deferred.reject);
    1645     return deferred.promise;
    1646 }
     1799};
    16471800
    16481801/**
    16491802 * Wraps a NodeJS continuation passing function and returns an equivalent
    16501803 * version that returns a promise.
    1651  *
    1652  *      Q.nfbind(FS.readFile, __filename)("utf-8")
    1653  *      .then(console.log)
    1654  *      .done()
    1655  *
    1656  */
    1657 Q.nfbind = nfbind;
    1658 Q.denodeify = Q.nfbind; // synonyms
    1659 function nfbind(callback/*, ...args */) {
     1804 * @example
     1805 * Q.nfbind(FS.readFile, __filename)("utf-8")
     1806 * .then(console.log)
     1807 * .done()
     1808 */
     1809Q.nfbind =
     1810Q.denodeify = function (callback /*...args*/) {
    16601811    var baseArgs = array_slice(arguments, 1);
    16611812    return function () {
     
    16631814        var deferred = defer();
    16641815        nodeArgs.push(deferred.makeNodeResolver());
    1665 
    1666         fapply(callback, nodeArgs).fail(deferred.reject);
     1816        Q(callback).fapply(nodeArgs).fail(deferred.reject);
    16671817        return deferred.promise;
    16681818    };
    1669 }
    1670 
    1671 Q.nbind = nbind;
    1672 function nbind(callback, thisArg /*, ... args*/) {
     1819};
     1820
     1821Promise.prototype.nfbind =
     1822Promise.prototype.denodeify = function (/*...args*/) {
     1823    var args = array_slice(arguments);
     1824    args.unshift(this);
     1825    return Q.denodeify.apply(void 0, args);
     1826};
     1827
     1828Q.nbind = function (callback, thisp /*...args*/) {
    16731829    var baseArgs = array_slice(arguments, 2);
    16741830    return function () {
     
    16761832        var deferred = defer();
    16771833        nodeArgs.push(deferred.makeNodeResolver());
    1678 
    16791834        function bound() {
    1680             return callback.apply(thisArg, arguments);
    1681         }
    1682 
    1683         fapply(bound, nodeArgs).fail(deferred.reject);
     1835            return callback.apply(thisp, arguments);
     1836        }
     1837        Q(bound).fapply(nodeArgs).fail(deferred.reject);
    16841838        return deferred.promise;
    16851839    };
    1686 }
     1840};
     1841
     1842Promise.prototype.nbind = function (/*thisp, ...args*/) {
     1843    var args = array_slice(arguments, 0);
     1844    args.unshift(this);
     1845    return Q.nbind.apply(void 0, args);
     1846};
    16871847
    16881848/**
     
    16951855 * @returns a promise for the value or error
    16961856 */
    1697 Q.npost = npost;
    1698 Q.nmapply = npost; // synonyms
    1699 function npost(object, name, args) {
     1857Q.nmapply = // XXX As proposed by "Redsandro"
     1858Q.npost = function (object, name, args) {
     1859    return Q(object).npost(name, args);
     1860};
     1861
     1862Promise.prototype.nmapply = // XXX As proposed by "Redsandro"
     1863Promise.prototype.npost = function (name, args) {
    17001864    var nodeArgs = array_slice(args || []);
    17011865    var deferred = defer();
    17021866    nodeArgs.push(deferred.makeNodeResolver());
    1703 
    1704     post(object, name, nodeArgs).fail(deferred.reject);
     1867    this.dispatch("post", [name, nodeArgs]).fail(deferred.reject);
    17051868    return deferred.promise;
    1706 }
     1869};
    17071870
    17081871/**
     
    17161879 * @returns a promise for the value or error
    17171880 */
    1718 Q.nsend = nsend;
    1719 Q.ninvoke = Q.nsend; // synonyms
    1720 Q.nmcall = Q.nsend; // synonyms
    1721 function nsend(object, name /*, ...args*/) {
     1881Q.nsend = // XXX Based on Mark Miller's proposed "send"
     1882Q.nmcall = // XXX Based on "Redsandro's" proposal
     1883Q.ninvoke = function (object, name /*...args*/) {
    17221884    var nodeArgs = array_slice(arguments, 2);
    17231885    var deferred = defer();
    17241886    nodeArgs.push(deferred.makeNodeResolver());
    1725     post(object, name, nodeArgs).fail(deferred.reject);
     1887    Q(object).dispatch("post", [name, nodeArgs]).fail(deferred.reject);
    17261888    return deferred.promise;
    1727 }
    1728 
     1889};
     1890
     1891Promise.prototype.nsend = // XXX Based on Mark Miller's proposed "send"
     1892Promise.prototype.nmcall = // XXX Based on "Redsandro's" proposal
     1893Promise.prototype.ninvoke = function (name /*...args*/) {
     1894    var nodeArgs = array_slice(arguments, 1);
     1895    var deferred = defer();
     1896    nodeArgs.push(deferred.makeNodeResolver());
     1897    this.dispatch("post", [name, nodeArgs]).fail(deferred.reject);
     1898    return deferred.promise;
     1899};
     1900
     1901/**
     1902 * If a function would like to support both Node continuation-passing-style and
     1903 * promise-returning-style, it can end its internal promise chain with
     1904 * `nodeify(nodeback)`, forwarding the optional nodeback argument.  If the user
     1905 * elects to use a nodeback, the result will be sent there.  If they do not
     1906 * pass a nodeback, they will receive the result promise.
     1907 * @param object a result (or a promise for a result)
     1908 * @param {Function} nodeback a Node.js-style callback
     1909 * @returns either the promise or nothing
     1910 */
    17291911Q.nodeify = nodeify;
    1730 function nodeify(promise, nodeback) {
     1912function nodeify(object, nodeback) {
     1913    return Q(object).nodeify(nodeback);
     1914}
     1915
     1916Promise.prototype.nodeify = function (nodeback) {
    17311917    if (nodeback) {
    1732         promise.then(function (value) {
     1918        this.then(function (value) {
    17331919            nextTick(function () {
    17341920                nodeback(null, value);
     
    17401926        });
    17411927    } else {
    1742         return promise;
    1743     }
    1744 }
     1928        return this;
     1929    }
     1930};
    17451931
    17461932// All code before this point will be filtered from stack traces.
Note: See TracChangeset for help on using the changeset viewer.