🐛 Fix promise not resolving after file download
This commit is contained in:
parent
6394de57f4
commit
d60c9097ab
1 changed files with 3 additions and 15 deletions
|
@ -12,25 +12,13 @@ export default function saveResponse(
|
|||
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);
|
||||
|
||||
//If we receive a part of the response, write it to disk
|
||||
response.pipe(writeStream);
|
||||
|
||||
//If we finished reading response, check for correctness, then return it
|
||||
response.on('end', () => {
|
||||
//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);
|
||||
});
|
||||
//Wait until everything is written to disk, then return file name
|
||||
writeStream.on('close', () => {
|
||||
resolve(filePath);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue