Skip to content

Instantly share code, notes, and snippets.

@adoji92

adoji92/1.js

Last active Feb 14, 2018
Embed
What would you like to do?
dfdf
async downloadSomething(mediaUrlList) {
mediaUrlList.forEach(async mediaUrl => {
const res = await axios.get(mediaUrl, { responseType: 'arraybuffer' });
fs.writeFileSync(...);
await delay(2000);
});
}
async function test(urlList) {
const promiseList = urlList.map(url => async () => axios.get(url));
return promiseList.reduce((p, fn) => p.then((value) => {
console.log('value', value);
return delay(2000);
}).then(fn), Promise.resolve());
}
test(['http://ip.jsontest.com', 'http://headers.jsontest.com', 'http://time.jsontest.com'])
.then((value) => {
console.log('value last', value.data);
});
async function test(urlList) {
if (urlList.length === 0) {
return;
}
const errorUrlList = [];
for (let i = 0; i < urlList.length; i += 1) {
const url = urlList[i];
try {
const res = Math.random() > 0.5 ? await axios.get(url) : await axios.get(1);
console.log(res.data);
} catch (e) {
errorUrlList.push(url);
} finally {
await delay(2000); // eslint-disable-line no-await-in-loop
}
}
}
test(errorUrlList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment