Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active November 9, 2017 10:11
Show Gist options
  • Save iegik/3a69da2034156d52b858096ba3aa26f6 to your computer and use it in GitHub Desktop.
Save iegik/3a69da2034156d52b858096ba3aa26f6 to your computer and use it in GitHub Desktop.
Fast Promise
const TIME_LIMIT = 1000;
const TIME_LIMIT_MESSAGE = 'Out of time limit';
export default p => (...args) => Promise.race([p(...args),new Promise((y,n) => setTimeout(n, TIME_LIMIT, TIME_LIMIT_MESSAGE))]);

Here is example of fast implimentation with requests:

import fast, request from './';
const {log} = console;

class API {}
API.json = (...args) => 
  // @fast request
  fast(request)(...args).then(data => data.json());

API.get = url => API.json(url)

(new API()).get('/')
  // Success
  .then(data => log('ok', {data}))
  // Fail
  .catch(e => log('nok', {errors:[e]}))
  // Finally
  .then(() => log('done'));
export default (...args) => {
try {
return fetch(...args)
} catch (e) {
return Promise.reject(e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment