Created
January 9, 2018 16:29
-
-
Save javimosch/68c3b0ea22c65fc5bfa66d03d7c81e42 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 sequence(list, results, resolve, reject) { | |
if (!results) { | |
return new Promise((resolve, reject) => sequence(list, [], resolve, reject));//first time | |
} else { | |
if (!results){ | |
results = [];//one time | |
} | |
if (list.length == 0) { | |
return resolve(results);//last time | |
} else { | |
list[0]().then(result => { //each time | |
results.push(result); | |
list.splice(0, 1); | |
sequence(list, results, resolve, reject); | |
}).catch(err => { | |
reject(err); | |
}); | |
} | |
} | |
} | |
//Testing | |
/* | |
sequence([ | |
() => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('Foo'); | |
resolve('Foo rocks!'); | |
},2000); | |
}), | |
() => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('Bar'); | |
resolve('Bar completed'); | |
},3000); | |
}) | |
]).then(results => { | |
console.log('YES!', results) | |
}) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment