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 allSequential(promiseFns) { | |
const initPromise = Promise.resolve([]); | |
return promiseFns.reduce(_chainPromise, initPromise); | |
function _chainPromise(acc, pFn) { | |
return acc.then(result => pFn().then(pResult => [...result, pResult])); | |
} | |
} | |
allSequential([ |
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
return Promise.all([ | |
task1(), | |
task2(), | |
task3() | |
]).then(([r1,r2,r3]) => { | |
// every promise was fulfilled | |
// do work with r1, r2 and r3 | |
// results are granted to be sorted as received | |
}) | |
.catch(err => { |
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 doSomething() { | |
const results = {}; | |
return performStep1() | |
.then(result1 => (results.one = result1)) | |
.then(() => performStep2(results.one)) | |
.then(result2 => (results.two = result2)) | |
.then(() => performStep3(results.two)) | |
.then(result3 => performFinalStep(results.one, result3)) | |
.then(finalResult => ({ | |
result2: results.two, |
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 doSomething() { | |
return performStep1() | |
.then(result1 => performStep2(result1)) | |
.then(result2 => performStep3(result2)) | |
.then(result3 => performFinalStep(result3)); | |
} |
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 doSomething() { | |
return performStep1().then(result1 => | |
performStep2(result1).then(result2 => | |
performStep3(result2).then(result3 => | |
performFinalStep(result3) | |
) | |
) | |
); | |
} |
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 wrapper() { | |
return new Promise((resolve, reject) => { | |
doNodeStuff((error, result) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result); | |
} | |
}); | |
}); |
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 doSomething(input) { | |
if (!input) { | |
return Promise.reject('missing input'); // returns a rejected promise | |
} else { | |
return Promise.resolve(input + ' done'); // returns a fulfilled promise | |
} | |
} |
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 doSomething(input) { | |
return new Promise((resolve, reject) => { | |
if (!input) { | |
reject('missing input'); | |
} else { | |
resolve(input + ' done'); | |
} | |
}); | |
} |
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 doSomething() { | |
return fetch('http://example.com/movies.json') | |
.then(response => response.json()); | |
} |
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 doSomething() { | |
return new Promise((resolve, reject) => { | |
fetch('http://example.com/movies.json') | |
.then(response => resolve(response.json())) | |
.catch(err => reject(err)); | |
}); | |
} |
NewerOlder