skerla-callbacks
v0.1.3
Published
callbacks with a timeout
Downloads
1
Readme
Callbacks
Quick example:
const co = require('co');
const Callbacks = require('skerla-callbacks');
//create an instance of a Callbacks class with namespace
const callbacks = Callbacks.init('scope1');
co(function *() {
//define data to work with
const ID = 10432;
const DATA = 'data1';
//create a callback
const promise = callbacks.create({
id: ID
});
//id is passed back. useful when id is autogenerated
promise.id.should.equals(ID);
//call callback providin ID and DATA in the future
setTimeout(function () {
callbacks.success(ID, DATA);
}, 100);
//wait for the response
const result = yield promise;
result.should.equals(DATA); //true
}).catch(err => console.error(err.stack));
API
Constructor
new Callbacks(scope: String): Callbacks
Static methods
Callbacks.init(scope: String): Callbacks
Instance methods
create([options: Options]): Promise
define Options {
[id]: Mixed //callback id. defaults to objectid()
[timeout]: Number //callback timeout in ms. defaults to 30 seconds
[name]: String //optinal callback name
}
define Promise {
id: Mixed //callback id
clear: Function //cancel callback timeout
}
success(id: Mixed, data: Mixed): Boolean
Returns true if callback id was found
fail(id: Mixed, data: Mixed): Boolean
Returns true if callback id was found