Skip to content

Instantly share code, notes, and snippets.

@glebcha
Last active May 25, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glebcha/7dae53b13050a3448b00677fef523442 to your computer and use it in GitHub Desktop.
Save glebcha/7dae53b13050a3448b00677fef523442 to your computer and use it in GitHub Desktop.
Fast concept of failed promise detection
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