Skip to content

Instantly share code, notes, and snippets.

@victorchee
Last active March 19, 2020 09:50
Show Gist options
  • Save victorchee/72f7009656b7eb0a888db4f1586a5b6c to your computer and use it in GitHub Desktop.
Save victorchee/72f7009656b7eb0a888db4f1586a5b6c to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
if (!fs.existsSync('images')) {
fs.mkdirSync('images');
}
async function download(url) {
const request = await fetch(url);
const filePath = './images/' + path.basename(url)
const stream = fs.createWriteStream(filePath);
return new Promise((resolve, reject) => {
request.body.pipe(stream);
request.body.on('error', error => {
reject(error);
});
stream.on('finish', () => {
resolve();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment