evt-promise
![Coverage Status](https://coveralls.io/repos/github/KsRyY/evt-async/badge.svg?branch=master)
An event-driven promise-like function wrapper, has a different api through the ES6 Promise
Install
npm install evt-promise
Usage
BASIC:
const async = require('evt-async');
var foo = async(function() {return true;});
foo.exec(function(value){console.log(value);},function(e) {throw e;});
var bar = async(function() {throw new Error('TEST');});
bar.exec(function(v){}, function(e){console.error(e.message);});
var baz = async(function(arg) {return arg;},[true]);
baz.exec(function(value){console.log(value);},function(e) {throw e;});
Advanced - Change the task that you want to run, Change the arguments that you want to pass to the task function:
const async = require('evt-async');
var foo = async(function() {return true});
foo.task = function() {
return false;
}
foo.exec(function(value){console.log(value);},function(e) {throw e;});
var bar = async(function(arg) {return arg;},[true]);
bar.args = [false];
bar.exec(function(value){console.log(value);},function(e) {throw e;});
Advanced - Emit custom events in the task and handle it
const async = require('evt-async');
var fn = function() {
this.emit('custom','custom event');
return 'hello custom';
};
var resHandler = function(value) {
console.log(value);
};
var rejHandler = function(e) {
throw e;
};
var foo = async(fn)
foo.event.on('custom', function(value) {console.log(value);});
foo.exec(resHandler,rejHandler);
Warning: If you use arrow function as the task, this example won't work because you cannot inject a this object to an arrow function