future v2.3.1
Future
Creates a Future (aka Promise, Deferred, Subscription, Callback) object.
Installation
Node.JS (Server):
npm install futureEnder.JS (Browser):
ender build futureUsage
var context = { "foo": "bar" }
, Future = require('future')
, future = Future(context)
, err
, message = "Hello World!"
;
future.whenever(function (error, data) {
if (error) {
throw err;
}
console.log(this.foo + " says: " + data);
});
future.setTimeout(100);
future.deliver(err, message);Output:
"bar says: Hello World"
FutureTimeout: timeout 100ms
at [object SomeObject]:x:y
...API
Core
Futures.future(globalContext=null)- creates aFutureobject and usesglobalContextas the defaultthisfor callbacksdeliver(err, data, ...)- Send a message (data) to all listeners (callbacks)fulfill([err, data, ...])- Prevent the sending of any future messages. If arguments are passed they will bedelivered.whenever(callback, [context])- Listen to all messages, applyingcontextif provided (passingnullcancelsglobalContext)when(callback, [context])- Listen one-time only, thenremoveCallbackautomaticallysetTimeout(ms)- will sends aFutureTimeouterror if no activity occurs withinms
Accessory
errback(err)- Useful for a peer-library requiring a method specifically for errbacks- i.e.
jQuery's$.ajax
- i.e.
callback(data [, ...])- Useful for a peer-library requiring a method which does not passerras the first parameter- i.e.
jQuery's$.ajax
- i.e.
removeCallback(callback, context=null)- This callback and associated context will no longer receive messagessetAsap(on=true)- New listeners get existing data (if available) rather than waiting until the next delivery (default on)isFuture(obj)- a best-effort guess as to whether or not an object is a FuturecallbackCount(callback, context)- The number of listening callbacksdeliveryCount(callback, context)- The number of deliveries madehasCallback(callback, context=null)- Returnstrueif the callback is listening