Last active
February 24, 2018 21:54
-
-
Save kukiron/4543af67b521e78e9fcfb2ec46fcc427 to your computer and use it in GitHub Desktop.
Function for checking whether setTimeout occurs for a network call
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 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