1.3.1 • Published 8 years ago
promise-stack v1.3.1
promise-stack
honeo/promise-stack
promise-stack
なにこれ
Promiseインスタンスを返す関数スタック。
setした関数を順に実行する。
実行した関数がPromiseインスタンスを返せばそのresolveまで待ち、違えば即座に次の関数を実行する。
使い方
$ npm i promise-stack
const PS = require('promise-stack');
const ps = new PS();
// example: normal function
ps.set(function(){
return 'hoge';
}).then( (arg)=>{
console.log(arg); //"hoge"
});
// example: promise function
function setTimeoutPromise(){
return new Promise( (resolve, reject)=>{
setTimeout(resolve, 1000);
});
}
for(let i=0; i<60; i++){
ps.set(setTimeoutPromise).then( (arg)=>{
console.log(i); //0...59, lastly after one minute
});
}
Method
set(function , {...})
引数の関数をスタックに積んで対になるPromiseインスタンスを返す。
const promise = instance.set(function);
option
instance.set(function, {
priority: number //0...
})
size()
Return number of queue.
clear()
Reject() all of queue.
start(), stop()
Switch of stack.
Properties
priority
指定のない場合のスタックの優先度、標準は5。
Event
primus/eventemitter3 is base.
instance.on('type', (e)=>{
console.log(e); //Object{...}
});
type : timing
set : #set() execution after.
clear : #clear() execution after.
empty : when stack of empty.
exec : queue(function) execution after.
start : #start() execution after.
stop : #stop() execution after.