From fdddfc9f779180beb863afc687cfab0a62255147 Mon Sep 17 00:00:00 2001 From: kriskowal Date: Wed, 22 Dec 2010 13:42:51 -0800 Subject: [PATCH] Added a send method to the public API, presently undocumented. --- CHANGES | 4 ++++ lib/q.js | 28 ++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index efb73037..60afd599 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,8 @@ +Next + - Added a send(value, op, args) method to the public API, for + forwarding messages to a value or promise in a future turn. + 0.1.9 - Added isRejected() for testing whether a value is a rejected promise. isResolved() retains the behavior of stating diff --git a/lib/q.js b/lib/q.js index abf4ca1d..2dbfd817 100644 --- a/lib/q.js +++ b/lib/q.js @@ -353,19 +353,31 @@ exports.asap = function (value, resolved, rejected) { * "Method" constructs methods like "get(promise, name)" and "put(promise)". */ exports.Method = Method; -function Method (methodName) { +function Method (op) { return function (object) { - var deferred = defer(); var args = Array.prototype.slice.call(arguments, 1); - forward.apply(undefined, [ - ref(object), - methodName, - deferred.resolve - ].concat(args)); - return deferred.promise; + return send(object, op, args); }; } +/** + * sends a message to a value in a future turn + * @param object* the recipient + * @param op the name of the message operation, e.g., "when", + * @param args {Array} an array of further arguments + * @returns result {Promise} a promise for the result of the operation + */ +exports.send = send; +function send(object, op, args) { + var deferred = defer(); + forward.apply(undefined, [ + ref(object), + op, + deferred.resolve + ].concat(args)); + return deferred.promise; +} + /** * Gets the value of a property in a future turn. * @param object promise or immediate reference for target object