Last active
September 12, 2018 10:53
-
-
Save ufocoder/a0a87ca8f8550ee83a24e9d1833edf3c to your computer and use it in GitHub Desktop.
getLastResolvedPromise
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
| import getLastResolvedPromise from './getLastResolvedPromise' | |
| const promise1 = Promise.resolve(3); | |
| const promise2 = new Promise(function(resolve, reject) { | |
| setTimeout(resolve, 200, 'foo'); | |
| }); | |
| const promise3 = new Promise(function(resolve, reject) { | |
| setTimeout(resolve, 100, 'bar'); | |
| }); | |
| getLastResolvedPromise([promise1, promise2, promise3]).then(function(lastResult) { | |
| console.log('lastResult', lastResult); | |
| }); |
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
| let lastResult | |
| const withSaveResult = promise => ( | |
| promise.then(result => { | |
| lastResult = result | |
| }) | |
| ) | |
| const getLastResolvedPromise = promises => { | |
| lastResult = null | |
| return Promise.all(promises.map(withSaveResult)).then(() => lastResult) | |
| } | |
| export default getLastResolvedPromise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment