Created
November 16, 2021 07:45
-
-
Save monjer/2cd2aec9843ba6788332bc3ee229b088 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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); | |
| } | |
| }) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.