import { FETCH_TIMEOUT } from 'constants/errors'; export default function fetchWithTimeout(ms, promise) { return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { reject(new Error(FETCH_TIMEOUT)); }, ms); promise.then( (res) => { clearTimeout(timeoutId); resolve(res); }, (err) => { clearTimeout(timeoutId); reject(err); } ); }); }