ES6 Promise + Thunk = PromiseThunk!
promise-thunk
is standard ES6 Promise implementation + thunk.
it supports browsers and node.js.
it throws unhandled rejection error.
INSTALL:
for node.js:
$ npm install promise-thunk --save
or
for browsers:
https://lightspeedworks.github.io/promise-thunk/promise-thunk.js
<script src="https://lightspeedworks.github.io/promise-thunk/promise-thunk.js"></script>
PREPARE:
you can use PromiseThunk.
in node.js
var PromiseThunk = require('promise-thunk');
in browsers
(function (PromiseThunk) {
'use strict';
})(this.PromiseThunk || require('promise-thunk'));
or
var PromiseThunk = this.PromiseThunk || require('promise-thunk');
QUICK EXAMPLE:
var PromiseThunk = require('promise-thunk');
function sleepNodeStyle(msec, val, cb) {
console.log(val + ' timer start: ' + msec);
setTimeout(function () {
console.log(val + ' timer end : ' + msec);
cb(null, val);
}, msec);
}
var sleepPromiseThunk = PromiseThunk.wrap(sleepNodeStyle);
sleepPromiseThunk(1000, 'a1')
.then(
function (val) {
console.log('a2 val: ' + val);
return sleepPromiseThunk(1000, 'a2'); },
function (err) { console.log('err:' + err); })
.catch(
function (err) { console.log('err:' + err); }
);
sleepPromiseThunk(1000, 'b1')
(function (err, val) {
console.log('b2 val: ' + val + (err ? ' err: ' + err : ''));
return sleepPromiseThunk(1000, 'b2'); })
(function (err, val) {
console.log('b3 val: ' + val + (err ? ' err: ' + err : '')); });
sleepPromiseThunk(1000, 'c1')
(function (err, val) {
console.log('c2 val: ' + val + (err ? ' err: ' + err : ''));
return sleepPromiseThunk(1000, 'c2'); })
.then(
function (val) {
console.log('c3 val: ' + val);
return sleepPromiseThunk(1000, 'c3'); },
function (err) { console.log('err:' + err); });
USAGE:
PromiseThunk Specification
new PromiseThunk(setup)
how to make promise.
p = new PromiseThunk(
function setup(resolve, reject) {
try { resolve('value'); }
catch (error) { reject(error); }
}
);
example
var p = new PromiseThunk(
function setup(resolve, reject) {
setTimeout(function () {
if (Math.random() < 0.5) resolve('value');
else reject(new Error('error'));
}, 100);
}
);
p.then(
function (val) { console.info('val:', val); },
function (err) { console.error('err:', err); });
p(function (err, val) { console.info('val:', val, 'err:', err); });
p(function (err, val) { console.info('val:', val, 'err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
.then(
function (val) { console.info('val:', val); },
function (err) { console.error('err:', err); })
.then(
function (val) { console.info('val:', val); },
function (err) { console.error('err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
.then(
function (val) { console.info('val:', val); },
function (err) { console.error('err:', err); });
PromiseThunk.convert(p)
convert()
converts standard promise to promise-thunk.
p
: standard promise or thenable object.
PromiseThunk.promisify([ctx,] fn, [options])
promisify()
converts node style function into a function returns promise-thunk.
you can use fs.exists()
and child_process.exec()
also.
ctx
: context object. default: this or undefined.fn
: node-style normal function.options
: options object.
also thenable, yieldable, callable.
postgres pg
example:
var pg = require('pg');
var pg_connect = PromiseThunk.promisify(pg, pg.connect);
var client_query = PromiseThunk.promisify(client, client.query);
PromiseThunk.promisify(object, method, [options])
promisify()
defines method promisified function returns promise-thunk.
object
: target object.method
: method name string.options
: method name suffix or postfix. default: 'Async'. or options object.
suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
postgres pg
example:
var pg = require('pg');
PromiseThunk.promisify(pg, 'connect', {suffix: 'A'});
PromiseThunk.promisify(pg.Client.prototype, 'connect');
PromiseThunk.promisify(pg.Client.prototype, 'query');
PromiseThunk.promisifyAll(object, [options])
promisifyAll()
defines all methods promisified function returns promise-thunk.
object
: target object.options
: method name suffix or postfix. default: 'Async'. or options object.
suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
file system fs
example:
var fs = require('fs');
PromiseThunk.promisifyAll(fs, {suffix: 'A'});
postgres pg
example:
var pg = require('pg');
PromiseThunk.promisifyAll(pg.constructor.prototype, {suffix: 'A'});
PromiseThunk.promisifyAll(pg.Client.prototype);
PromiseThunk.thunkify([ctx,] fn, [options])
thunkify()
converts node style function into a thunkified function.
you can use fs.exists()
and child_process.exec()
also.
ctx
: context object. default: this or undefined.fn
: node-style normal function with callback.options
: options object.
also yieldable, callable.
postgres pg
example:
var pg = require('pg');
var pg_connect = thunkify(pg, pg.connect);
var client_query = thunkify(client, client.query);
PromiseThunk.thunkify(object, method, [options])
thunkify()
defines method thunkified function returns thunk.
object
: target object.method
: method name string.options
: method name suffix or postfix. default: 'Async'. or options object.
suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
postgres pg
example:
var pg = require('pg');
PromiseThunk.thunkify(pg, 'connect', {suffix: 'A'});
PromiseThunk.thunkify(pg.Client.prototype, 'connect');
PromiseThunk.thunkify(pg.Client.prototype, 'query');
PromiseThunk.thunkifyAll(object, [options])
thunkifyAll()
defines all methods thunkified function returns thunk.
object
: target object.options
: method name suffix or postfix. default: 'Async'. or options object.
suffix
: method name suffix or postfix. default: 'Async'.postfix
: method name suffix or postfix. default: 'Async'.
file system fs
example:
var fs = require('fs');
PromiseThunk.thunkifyAll(fs, {suffix: 'A'});
postgres pg
example:
var pg = require('pg');
PromiseThunk.thunkifyAll(pg.constructor.prototype, {suffix: 'A'});
PromiseThunk.thunkifyAll(pg.Client.prototype);
promise.then(onFulfilled, onRejected)
how to use promise.
p = p.then(
function resolved(value) {},
function rejected(error) {});
example
p = p.then(
function resolved(value) {
console.info(value);
},
function rejected(error) {
console.error(error);
});
promise.catch(onRejected)
how to catch error from promise.
p = p.catch(
function rejected(error) {});
or
when you use old browser
p = p['catch'](
function rejected(error) {});
PromiseThunk.all(iterable or array)
wait for all promises.
p = PromiseThunk.all([promise, ...]);
PromiseThunk.race(iterable or array)
get value or error of first finished promise.
p = PromiseThunk.race([promise, ...]);
PromiseThunk.resolve(value or promise)
get resolved promise.
p = PromiseThunk.resolve(value or promise);
PromiseThunk.reject(error)
get rejected promise.
p = PromiseThunk.reject(error);
PromiseThunk.accept(value)
get resolved (accepted) promise.
p = PromiseThunk.accept(value);
PromiseThunk.defer()
make deferred object with promise.
dfd = PromiseThunk.defer();
SEE ALSO:
npm promise-light
npm aa
npm co
LICENSE:
MIT