fetch() timeout

Since fetch() doesn’t provide any way for setting a timeout for requests by design, I’ve implemented a simple wrapper Promise based solution which might come handy for your remote API calls.

var FETCH_TIMEOUT = 5000;
new Promise(function(resolve, reject) {
    var timeout = setTimeout(function() {
        reject(new Error('Request timed out'));
    .then(function(response) {
        if (response && response.status == 200) return response.json();
        else reject(new Error('Response error'));
    .then(function(responseObject) {
        // process results
    .catch(function(err) {
.then(function() {
    // request succeed
.catch(function(err) {
    // error: response error, request timeout or runtime error

Nice one :slight_smile: We need to do something about the timeout situation

Thanks:) It’s still not perfect, though.

What about the timer API? I am using it and looks like it works