I read through a wide variety of different questions on using axios/fetch/requests to retrieve data from a list of urls (api calls). I realize similar questions have been asked before. I have some code that almost works but I cannot figure out how to delay between the calls. My timeout doesn't work how I want it to. I want it to wait 3000 ms between calls.
const request = require('request');
var requestAsync = function(url) {
return new Promise((resolve, reject) => setTimeout(() => {
var req = request(url, (err, response, body) => {
if (err) return reject(err, response, body);
resolve(JSON.parse(body));
});
},3000)) ;
};
const urls = [
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/albums',
'https://jsonplaceholder.typicode.com/users'
];
var fetchURLdata = async function() {
try {
var data = await Promise.all(urls.map(requestAsync));
console.log(data)
} catch (err) {
console.error(err);
}
console.log(data);
}
fetchURLdata();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…