Skip to content

Instantly share code, notes, and snippets.

@miconblog
Created November 7, 2014 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miconblog/c47900fcd2adc0f7df6f to your computer and use it in GitHub Desktop.
Save miconblog/c47900fcd2adc0f7df6f to your computer and use it in GitHub Desktop.
execute promise collections
/**
* Promises 객체를 순차적으로 실행시켜주는 파이프 라인 클래스
* .loop() - 무한 루프로 돌려준다. (default:0)
* .loop(1) - 루프를 한번만 돈다.
*/
function noop (){}
module.exports = function Pipeline (promiseArray, params) {
var promises = promiseArray;
var options = _.extend({
pipelineName : 'Name for me!',
repeatMode : false,
breakMode: false,
cbFail : noop,
cbPass : noop,
cbEnd : noop
}, params);
this.loop = function(limitCount){
options.repeatMode = !limitCount;
excuteLoop(0);
};
function excuteLoop(loopCount){
if( options.repeatMode ){
if( loopCount === promises.length ){
loopCount = 0;
}
}
if(loopCount < promises.length){
promises[loopCount]()
.then(function(data){
if( data === 'retry') {
excuteLoop(loopCount);
return;
}
options.cbPass(data);
excuteLoop(loopCount + 1);
})
.fail(function(err){
options.cbFail(err);
if(!breakMode){
excuteLoop(loopCount + 1);
}
});
}else{
console.log('pipeline terminated....');
options.cbEnd();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment