1export async function downloadFile(fileUrl: string, outputLocationPath: string) {
2 const writer = createWriteStream(outputLocationPath);
3
4 return Axios({
5 method: 'get',
6 url: fileUrl,
7 responseType: 'stream',
8 }).then(response => {
9
10 //ensure that the user can call `then()` only when the file has
11 //been downloaded entirely.
12
13 return new Promise((resolve, reject) => {
14 response.data.pipe(writer);
15 let error = null;
16 writer.on('error', err => {
17 error = err;
18 writer.close();
19 reject(err);
20 });
21 writer.on('close', () => {
22 if (!error) {
23 resolve(true);
24 }
25 //no need to call the reject here, as it will have been called in the
26 //'error' stream;
27 });
28 });
29 });
30}