🐛 Fix promise not resolving after file download

This commit is contained in:
C-3PO 2018-09-30 14:38:46 +02:00
parent 6394de57f4
commit d60c9097ab
Signed by: c3po
GPG key ID: 62993C4BB4D86F24

View file

@ -12,25 +12,13 @@ export default function saveResponse(
return reject(`Expected status code 200 but received ${response.statusCode}.`); return reject(`Expected status code 200 but received ${response.statusCode}.`);
} }
//Remember file size
//const headerLength = Number(response.headers['content-length']);
const writeStream = fs.createWriteStream(filePath); const writeStream = fs.createWriteStream(filePath);
//If we receive a part of the response, write it to disk //If we receive a part of the response, write it to disk
response.pipe(writeStream); response.pipe(writeStream);
//If we finished reading response, check for correctness, then return it //Wait until everything is written to disk, then return file name
response.on('end', () => { writeStream.on('close', () => {
//Check that length is correct
/*if (totalLength !== headerLength) {
return reject(`Expected length ${headerLength} but received ${totalLength}.`);
}*/
//wait until everything is written to disk, then return file name
writeStream.end(() => {
//TODO: this is no longer firing ever since we replaced on('data') event by pipe()
resolve(filePath); resolve(filePath);
}); });
});
} }