ripunzel/fetch/item.js

26 lines
525 B
JavaScript

'use strict';
const fetch = require('node-fetch');
function fetchItem(url, attempt) {
function retry(error) {
console.log(error);
if(attempt < 3) {
console.log('Retrying...');
return fetchItem(url, ++attempt);
}
};
return fetch(url).then(res => {
return res.ok ? res : Promise.reject(`Failed to fetch ${url}`);
}).then(res => {
console.log(`Fetched '${url}'`);
return res.body;
}).catch(retry);
};
module.exports = fetchItem;