Merge pull request #523 from embark-framework/bug_fix/bad-branch

Fix callback already called on file download fail
This commit is contained in:
Iuri Matias 2018-06-13 14:42:08 -04:00 committed by GitHub
commit 977b472217
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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);