ginormous
ginormous

Reputation: 1

Node.js chaining promises but still executing out of order

Hello so i'm trying to execute 3 things in order.

  1. First then: generate images
  2. Second then: zip them up
  3. Third then: email them

Generating and zipping the images is working fine, but when I go to email them, the zip file hasn't been populated so it's just sending an empty zip. Any tips here?

rowData().then(res => {
        let functionArray;      
        if(client == 'TCS'){
            functionArray = TCSFunctionArray;
        } else{
            console.log('Please enter a valid client name');
        }
        
        let images = [];
        //generating all the images
        for(let i = 0; i < res.length; i++){
            for(let j = 0; j < functionArray.length; j++){
                images.push(nodeHtmlToImage({
                    output: `${PATH}/row_${i+2}_${functionArray[j].name}.png`,
                    html: functionArray[j](res[i]).html,
                    puppeteerArgs: {defaultViewport: {height: functionArray[j](res[i]).height, 
                                                        width: functionArray[j](res[i]).width, 
                                                        deviceScaleFactor: 2}, args: ["--no-sandbox"]}
                }))
            }
        }
        return Promise.all(images);
    }).then(() => {
       zipBanners(PATH, dest)
    }).then(() => {
        emailBanners(dest)
    })

function zipBanners(sourceFolder, dest){
    tar.c(
        {
          gzip: true
        },
        [sourceFolder]
      ).pipe(fs.createWriteStream(dest));
}

Upvotes: 0

Views: 29

Answers (1)

Alvin Abia
Alvin Abia

Reputation: 1266

If your zipBanners returns a promise, make sure you return it for your third then call:

return zipBanners(PATH, dest)

Otherwise, you're just saying to start running the zipBanners function and then immediately go to the emailing part of your promise chain even though zip file hasn't been populated yet.

Upvotes: 1

Related Questions