Last active
June 25, 2016 11:25
-
-
Save IUnknown68/bf570ad483e3b56267504387df7acf8d to your computer and use it in GitHub Desktop.
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
function step1(result) { | |
console.log('call step1'); | |
return new Promise(function(resolve, reject) { | |
console.log('resolve step1'); | |
result['step1'] = 'Step 1'; | |
return resolve(result); | |
}); | |
} | |
function step2(result) { | |
console.log('call step2'); | |
return new Promise(function(resolve, reject) { | |
console.log('resolve step2'); | |
// return reject(new Error('Step 2')); | |
result['step2'] = 'Step 2'; | |
return resolve(result); | |
}); | |
} | |
function step3(result) { | |
console.log('call step3'); | |
return new Promise(function(resolve, reject) { | |
console.log('resolve step3'); | |
result['step3'] = 'Step 3'; | |
return resolve(result); | |
}); | |
} | |
function resolved(result) { | |
console.log(`Done:`, result); | |
} | |
function failed(error) { | |
console.warn(error); | |
} | |
function runQueue(queue, result) { | |
return queue.reduce(function(p, c) { | |
return p.then(c); | |
}, Promise.resolve(result || {})); | |
} | |
/* | |
es6: | |
const runQueue = (queue, result) => | |
queue.reduce((p, c) => p.then(c), Promise.resolve(result || {})); | |
*/ | |
runQueue([step1, step2, step3]).then(resolved).catch(failed); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment