1.1.0 • Published 7 years ago

all-promise v1.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

Promise

Implementation of javascript Promise/A+ standard.

Utils to turn things to promise.

Promise API

Constructor
new Promise(fn: function(resolve, reject))
Promise Instance Methods
promise.then(onFulfilled: null|function(value), onRejected: null|function(reason))
promise.catch(onRejected: function(reason))
promise.finally(onRejected: function(valueOrReason))
Static Methods
Promise.resolve(value: *)
Promise.reject(reason: *)
Promise.all(promises: Array.<Promise | Thenable>)
Promise.race(promises: Array.<Promise | Thenable>)

Promise Translate Utils API

callback_to_promise(func, options?): Turn function using callback to function returning promise.

API:

// @param func: any func that receives callback as last argument and calling callback with error as first argument)
// @param [options]: options.resolvedAsArray (default false) means whether to resolve callback data as array. If false, only the first data is received
callback_to_promise(func, options?)

Examples:

////// Before transforming
func(a, b, c, function (error, data) { ... });
/*nodejs*/ child_process.exec('ls', function (error, stdout, stdin) { ... });

////// After transforming
callback_to_promise(func)(a, b, c)
    .then(function (data) { ... })
    .catch(function (error) { ... });
/*nodejs*/ callback_to_promise(child_process.exec, {resolvedAsArray: true})('ls')
    .then(function (dataArray) {
        var stdout = dataArray[0];
        var stdin = dataArray[1];
        ...
    })
    .catch(function (error) { ... });
1.1.0

7 years ago

1.0.0

8 years ago

0.1.0

8 years ago