Skip to content

Instantly share code, notes, and snippets.

@monjer
Created November 16, 2021 07:45
Show Gist options
  • Select an option

  • Save monjer/2cd2aec9843ba6788332bc3ee229b088 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/2cd2aec9843ba6788332bc3ee229b088 to your computer and use it in GitHub Desktop.
const https = require('https');
const fs = require('fs');
/**
*
* @param {string} url
* @param {string} dest
* @example
* (async () => {
* await download('https://img.alicdn.com/imgextra/i1/O1CN01MJQUsF1ag0hxvreSe_!!6000000003358-2-tps-1280-1120.png', 'a.png');
* console.log('xxx')
* })()
*
* @returns {Promise}
*/
const download = async (url, dest) => {
return new Promise((resovle, reject) => {
try {
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close(() => {
resovle()
});
});
});
} catch (error) {
reject(error);
}
})
}
@monjer

monjer commented Nov 16, 2021

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment