diff --git a/lib/core/file.js b/lib/core/file.js index 65e5e634..bc97ddaa 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -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);