fetch.js
function fetchFile(address, filepath) {
    return get(address)
        .then(res => new Promise((fulfill, reject) => {
            const file = fs.createWriteStream(filepath);
            res.on('error', err => {
                // reading error
                file.close();
                fs.unlink(filepath, err => {   // best effort
                    if (err) console.error(err);
                });
                reject(err);
            });
            file.on('error', err => {
                // writing error
                fs.unlink(filepath, err => {   // best effort
                    if (err) console.error(err);
                });
                reject(err);
            });
            file.on('finish', () => {
                fulfill(filepath);
            });
            res.pipe(file);
        }));
}
function fetchFile(address, filepath) {
    return get(address)
        .then(res => new Promise((fulfill, reject) => {
            const file = fs.createWriteStream(filepath);
            res.on('error', err => {
                // reading error
                file.close();
                fs.unlink(filepath, err => {   // best effort
                    if (err) console.error(err);
                });
                reject(err);
            });
            file.on('error', err => {
                // writing error
                fs.unlink(filepath, err => {   // best effort
                    if (err) console.error(err);
                });
                reject(err);
            });
            file.on('finish', () => {
                fulfill(filepath);
            });
            res.pipe(file);
        }));
}
function fetchFile ( address , filepath ) { return get ( address ) . then ( res => new Promise ( ( fulfill , reject ) => { const file = fs . createWriteStream ( filepath ) ; res . on ( 'error' , err => { // reading error file . close ( ) ; fs . unlink ( filepath , err => { // best effort if ( err ) console . error ( err ) ; } ) ; reject ( err ) ; } ) ; file . on ( 'error' , err => { // writing error fs . unlink ( filepath , err => { // best effort if ( err ) console . error ( err ) ; } ) ; reject ( err ) ; } ) ; file . on ( 'finish' , ( ) => { fulfill ( filepath ) ; } ) ; res . pipe ( file ) ; } ) ) ; }
Fetches file from the given address, creating a file
into the given file path.

@param {string} address source URL
@param {string} filepath destination file path
@returns {Promise<string>} promise resolving to the destination file path
Fetches file from the given address creating a file into the given file path .