Skip to content

Instantly share code, notes, and snippets.

@ufocoder
Last active September 12, 2018 10:53
Show Gist options
  • Select an option

  • Save ufocoder/a0a87ca8f8550ee83a24e9d1833edf3c to your computer and use it in GitHub Desktop.

Select an option

Save ufocoder/a0a87ca8f8550ee83a24e9d1833edf3c to your computer and use it in GitHub Desktop.
getLastResolvedPromise
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);
});
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