Yu-Jack
1/9/2018 - 12:37 PM

test.js

require('es6-promise').polyfill();
require('isomorphic-fetch');

// example 1 在第一個 then throw error
fetch('//offline-news-api.herokuapp.com/stories')
.then(function(response) {
    throw new Error("Bad response from first then");
})
.then(function(stories) {
    console.log(stories.length);
}).catch((error) => {
    console.log('Got you');
    console.log(error.message);
});


// example 2 在第一個 then throw error
fetch('//offline-news-api.herokuapp.com/stories')
.then(function(response) {
    if (response.status >= 400) {
        throw new Error("Bad response from server");
    }
    console.log('Go to next then');
    return response.json();
})
.then(function(stories) {
    throw new Error("Bad response from second then");
}).catch((error) => {
    console.log('Got you');
    console.log(error.message);
});