promise-thunk - npm
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
PREPARE:
you can use PromiseThunk.
in node.js
var PromiseThunk = ;
in browsers
{ 'use strict'; // you can use PromiseThunk}thisPromiseThunk || ;
or
var PromiseThunk = thisPromiseThunk || ;
QUICK EXAMPLE:
var PromiseThunk = ; { console; ;} var sleepPromiseThunk = PromiseThunk; // promise type; // thunk type { console; return ; } { console; }; // mixed promise and thunk type { console; return ; }; //output:// a1 timer start: 1000// b1 timer start: 1000// c1 timer start: 1000// a1 timer end : 1000// a2 val: a1// a2 timer start: 1000// b1 timer end : 1000// b2 val: b1// b2 timer start: 1000// c1 timer end : 1000// c2 val: c1// c2 timer start: 1000// a2 timer end : 1000// b2 timer end : 1000// b3 val: b2// c2 timer end : 1000// c3 val: c2// c3 timer start: 1000// c3 timer end : 1000
USAGE:
PromiseThunk Specification
new PromiseThunk(setup)
how to make promise.
p = { // async process -> resolve(value) or reject(error) try ; catch error ; };// setup(// function resolve(value) {},// function reject(error) {})
example
var p = { ; }; // promisep; // thunk; // mixed chainable { console; } { console; } { console; };
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.context
: context object.
also thenable, yieldable, callable.
pg
example:
postgres var pg = ;var pg_connect = PromiseThunk; // -> yield pg_connect()var client_query = PromiseThunk; // -> yield 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'.
pg
example:
postgres var pg = ;PromiseThunk; // -> yield pg.connectA()PromiseThunk; // -> yield client.connectAsync()PromiseThunk; // -> yield client.queryAsync()
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'.
fs
example:
file system var fs = ;PromiseThunk; // -> yield fs.readFileA()
pg
example:
postgres var pg = ;PromiseThunk; // -> yield pg.connectA()PromiseThunk; // -> yield client.connectAsync() // -> yield client.queryAsync()
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.context
: context object.
also yieldable, callable.
pg
example:
postgres var pg = ;var pg_connect = ; // -> yield pg_connect()var client_query = ; // -> yield 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'.
pg
example:
postgres var pg = ;PromiseThunk; // -> yield pg.connectA()PromiseThunk; // -> yield client.connectAsync()PromiseThunk; // -> yield client.queryAsync()
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'.
fs
example:
file system var fs = ;PromiseThunk; // -> yield fs.readFileA()
pg
example:
postgres var pg = ;PromiseThunk; // -> yield pg.connectA()PromiseThunk; // -> yield client.connectAsync() // -> yield client.queryAsync()
promise.then(onFulfilled, onRejected)
how to use promise.
p = p;
example
p = p;
promise.catch(onRejected)
how to catch error from promise.
p = p;
or
when you use old browser
p = p'catch' {};
PromiseThunk.all(iterable or array)
wait for all promises.
p = PromiseThunkallpromise ...;
PromiseThunk.race(iterable or array)
get value or error of first finished promise.
p = PromiseThunk;
PromiseThunk.resolve(value or promise)
get resolved promise.
p = PromiseThunk;
PromiseThunk.reject(error)
get rejected promise.
p = PromiseThunk;
PromiseThunk.accept(value)
get resolved (accepted) promise.
p = PromiseThunkacceptvalue;
PromiseThunk.defer()
make deferred object with promise.
dfd = PromiseThunkdefer;// -> {promise, resolve, reject}
SEE ALSO:
npm promise-light
npm aa
npm co
LICENSE:
MIT