fix callback already called on file download fail

This commit is contained in:
Jonathan Rainville 2018-06-13 14:37:15 -04:00
parent 0429703790
commit e1c5487ba4
1 changed files with 13 additions and 3 deletions

View File

@ -73,15 +73,25 @@ class File {
});
},
function downloadTheFile(next) {
let alreadyCalledBack = false;
function doCallback(err) {
if (alreadyCalledBack) {
return;
}
alreadyCalledBack = true;
next(err);
}
request(url)
.on('response', function (response) {
if (response.statusCode !== 200) {
next('Getting file returned code ' + response.statusCode);
doCallback('Getting file returned code ' + response.statusCode);
}
})
.on('error', next)
.on('error', doCallback)
.pipe(fs.createWriteStream(filename))
.on('finish', next);
.on('finish', () => {
doCallback();
});
},
function readFile(next) {
fs.readFile(filename, next);