Guest User

Untitled

a guest
Mar 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. const API_URL = '...';
  2. const TIMEOUT = 10; // seconds
  3.  
  4. export default (path) => {
  5. /* global fetch */
  6. const req = fetch(API_URL + path);
  7.  
  8. const timeout = new Promise((resolve, reject) => {
  9. return setTimeout(() => reject(new Error('request timeout')), TIMEOUT * 1000);
  10. });
  11.  
  12. return Promise.race([req, timeout]).then(response => response.json());
  13. };
Add Comment
Please, Sign In to add comment