Last active
August 25, 2016 04:10
-
-
Save goonoo/969fd3b62178e0702ab084f01e6861d4 to your computer and use it in GitHub Desktop.
ES6 promise best practices
This file contains hidden or 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
// 함수 호출 타이밍에 조건에 따라 promise 실행 분기 | |
// val1 == true, val3 == true인 경우: method1 -> method2 -> method3true | |
// val2 == true, val3 == false인 경우: method1 -> method2 -> method3false | |
// val1 == false, val3 == true인 경우: method2 -> method3true | |
// val2 == false, val3 == false인 경우: method2 -> method3false | |
function run(val1, val3) { | |
const p = Promise.resolve(null); | |
if (val1 === true) | |
p = p.then(method1); | |
p = p.then(method2); | |
p = p.then(val3 === true ? method3true : method3false); | |
return p; | |
} |
This file contains hidden or 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
// promise 함수 실행 결과에 따라 다른 promise 실행 분기 | |
// method1의 실행결과가 true: method2 -> method3 실행. 이 때 method3에 run 함수의 val1 인자 포함. | |
// method1의 실행결과가 false: null 응답 | |
functionn run(val1) { | |
return method1().then( (result1) => { | |
if (result1 === true) | |
return method2().then(method3.bind(null, val1)); | |
else | |
return null; | |
}); | |
} |
This file contains hidden or 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
// loop를 돌며 복수 method 동시 실행 | |
// 동시 실행 수(concurrency) 제어: bluebird 이용 http://bluebirdjs.com/docs/api/promise.map.html | |
function run1() { | |
const methods = [method1, method2, method3]; | |
return Promise.all( | |
methods.map( (method) => method() ) | |
); | |
} | |
// loop를 돌며 복수 method 순차 실행 | |
function run2() { | |
const methods = [method1, method2, method3]; | |
let p = Promise.resolve(null); | |
methods.forEach( (method) => { | |
p = p.then( method() ); | |
}); | |
return p; | |
} | |
// 동시 실행, 순차 실행을 섞어서 | |
// method1/method2/method3 => method4 => methodA => methodB => methodC => method8/method9 | |
function run3() { | |
return Promise.all( [method1(), method2(), method3()] ) | |
.then(method4) | |
.then( () => { | |
const serialMethods = [methodA, methodB, methodC]; | |
let p = Promise.resolve(null); | |
methods.forEach( (method) => { | |
p = p.then( method() ); | |
}); | |
return p; | |
} ) | |
.then(Promise.all( [method8(), method9()] )); | |
} |
This file contains hidden or 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
// 엄청 복잡한 패턴 (댓글 참조) | |
function run() { | |
const p1 = method1().then( Promise.all( [method2(), method3()] ) ); | |
const p2 = method5(); | |
const p3 = Promise.all( [method7(), method8(), method9()] ); | |
const p4 = p1.then( method4 ); | |
const p5 = Promise.all( [p1, p2] ).then( method6 ); | |
return Promise.all( [p3, p4, p5] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
promise4.js case is from: http://kivol.net/playnode.pdf