0

I am using axios to make get requests to an API for fetching data that I have to save to files.

I want to wait for the file to finish downloading before sending another request. Still new to async await and nodejs.

jobOutputArr contains an array of URLs and elements, and I am looping on all the URLs to get the data to write to files.

function downloadFiles(filename, url, headers) {
    axios({
       url: url,
       method: "GET",
       headers: headers,
       responseType: 'stream'
   })
   .then(function (response){
        console.log('Connecting …')
        var path = Path.resolve(__dirname, filename)
        var writer = Fs.createWriteStream(path)
        response.data.pipe(writer)
        console.log('File Downloaded…'+filename)
   })
    
}

app.get('/downloadFiles',  (req, res) => {

    var headers = {
        'Authorization': "Bearer " + accessTokenReceived,
        'Accept-Encoding': 'gzip'
    }

    //Downloading files

    let fileList = new Object();
    var filename

    fileList = {
        ExplanationOfBenefit : 0,
        Patient : 0,
        Coverage : 0
    }
    jobOutputArr.forEach( async element => {
        switch(element.type){
            case "ExplanationOfBenefit":
                filename = "ExplanationOfBenefit_"+fileList.ExplanationOfBenefit+".ndjson"
                fileList.ExplanationOfBenefit+=1
                console.log("Going in for "+filename)
                await downloadFiles(filename, element.url, headers)
                console.log("Coming out for "+filename)
                break;
            case "Patient":
                filename = "Patient_"+fileList.Patient+".ndjson"
                fileList.Patient+=1
                console.log("Going in for "+filename)
                await downloadFiles(filename, element.url, headers)
                console.log("Coming out for "+filename)
                break;
            case "Coverage":
                filename = "Coverage_"+fileList.Coverage+".ndjson"
                fileList.Coverage+=1
                console.log("Going in for "+filename)
                await downloadFiles(filename, element.url, headers)
                console.log("Coming out for "+filename)
                break;
        }
        
        
    })
    res.send("Done!")
})

Output I am getting:

Going in for Patient_0.ndjson
Going in for Coverage_0.ndjson
Going in for ExplanationOfBenefit_0.ndjson
Coming out for ExplanationOfBenefit_0.ndjson
Coming out for ExplanationOfBenefit_0.ndjson
Coming out for ExplanationOfBenefit_0.ndjson
Connecting …
File Downloaded…Patient_0.ndjson
Connecting …
File Downloaded…ExplanationOfBenefit_0.ndjson
Connecting …
File Downloaded…Coverage_0.ndjson

Output I wish:

Going in for Patient_0.ndjson
Connecting …
File Downloaded…Patient_0.ndjson
Coming out for Patient_0.ndjson
Going in for Coverage_0.ndjson
Connecting …
File Downloaded…Coverage_0.ndjson
Coming out for Coverage_0.ndjson
Going in for ExplanationOfBenefit_0.ndjson
Connecting …
File Downloaded…ExplanationOfBenefit_0.ndjson
Coming out for ExplanationOfBenefit_0.ndjson
ruth
  • 3
  • 2
  • `forEach` is not blocking. in your case above, you should use a `for` loop. also outlined in this answer https://stackoverflow.com/a/46086037/5927442 – LostJon Apr 26 '21 at 17:19

1 Answers1

0

try putting return before axios and response.data