Skip to content

Instantly share code, notes, and snippets.

@kukiron
Last active February 24, 2018 21:54
Show Gist options
  • Select an option

  • Save kukiron/4543af67b521e78e9fcfb2ec46fcc427 to your computer and use it in GitHub Desktop.

Select an option

Save kukiron/4543af67b521e78e9fcfb2ec46fcc427 to your computer and use it in GitHub Desktop.
Function for checking whether setTimeout occurs for a network call
function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutID = setTimeout(() => {
reject(new Error("TIMEOUT!!"))
}, ms)
promise.then(res => {
clearTimeout(timeoutID)
resolve(res)
}, err => {
clearTimeout(timeoutID)
reject(err)
})
})
}
const request = fetch("https://api-clayshop.herokuapp.com/users")
timeoutPromise(50, request).then(res => res.json()).then(users => {
console.log(users)
}).catch(err => {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment