Chain then and catch
Each then receives the result of the one before. One catch handles all errors.
AJAX · Chain then and catchBeginner
fetch('/api/items')
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
})
.then(function (data) {
return data.items.length;
})
.then(function (count) {
console.log(count + ' items');
})
.catch(function (err) {
console.error(err);
});fetchpromisethencatch