Created
March 6, 2023 09:27
-
-
Save adnanirfan/d465f41f936be161bd0c7d5eb53b2393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import fs from 'fs'; | |
const images = ["https://avatars.githubusercontent.com/u/9200214?s=500&v=2"]; | |
(async () => { | |
function getImage(url, index) { | |
try { | |
axios({ | |
method: 'get', | |
url, | |
responseType: 'stream', | |
}) | |
.then((response) => { | |
// Pipe the image stream to a file | |
response.data.pipe(fs.createWriteStream(`image-${index}.jpg`)); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
} catch (error) { | |
console.log('===== ERROR:', error); | |
} | |
} | |
for (let index in images) { | |
console.log(index); | |
getImage(images[index], index); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment