🐛 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}.`);
|
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
|
resolve(filePath);
|
||||||
/*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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue