Created
November 7, 2014 08:59
-
-
Save miconblog/c47900fcd2adc0f7df6f to your computer and use it in GitHub Desktop.
execute promise collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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