Last active
May 25, 2021 14:48
-
-
Save glebcha/7dae53b13050a3448b00677fef523442 to your computer and use it in GitHub Desktop.
Fast concept of failed promise detection
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
function bulkRequest(urls) { | |
const promises = urls.reduce((result, url) => { | |
const isValidUrl = Object.prototype.toString.call(url) === '[object String]'; | |
if (isValidUrl) { | |
result.push( | |
fetch(url) | |
.catch(({ message }) => console.warn(`Bulk Request: ${ message }`)) | |
.then(response => ({ [url]: response })) | |
) | |
} | |
return result | |
}, []) | |
return Promise.all(promises) | |
} | |
const urls = ['https://reqres.in/api/unknown', 'https://reqres.in/api/users/23', 'http://httpstat.us/500']; | |
bulkRequest(urls).then(data => console.info('DATA:', data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment