From d60c9097ab99ac67bbf613423d9f91882e23547b Mon Sep 17 00:00:00 2001 From: C-3PO Date: Sun, 30 Sep 2018 14:38:46 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20promise=20not=20resolving?= =?UTF-8?q?=20after=20file=20download?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cdn/funcs/saveResponse.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/cdn/funcs/saveResponse.ts b/src/cdn/funcs/saveResponse.ts index 2daa6cd..00d5f51 100644 --- a/src/cdn/funcs/saveResponse.ts +++ b/src/cdn/funcs/saveResponse.ts @@ -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); }); }