0

I have a function where it will read excel document and write the contents to a file. Once the writing is done I will call resolve and the caller function will download the file to the user.

But resolve is getting called before completing other functions as a result the file will be empty when downloaded.

var downloadFile = (oracledb, connectionAttributes, responsem, files) => {
return new Promise((resolve, reject) => {
    oracledb.getConnection(connectionAttributes, (error, connection) => {
        var completed = false;
        if (error) {
            reject(response.send(`Cannot establish connection${JSON.stringify(connectionAttributes,undefined,2)}`));
            return;
        } else if (connection) {
            //result will have all the sheets data.

            result[sheet1].forEach((element) => {
               //code to add all the data to file by reading sheet data.
            });

            result[sheet2].forEach((element) => {
                //code to add all the data to file by reading sheet data.
            });
            resolve();
        }
    });
});}


caller function code-> downloadFile().then((resolved) => {
            response.download(filePath);
        }, (reject) => {
            console.log(reject);
        });
MT0
  • 143,790
  • 11
  • 59
  • 117
Prajyod Kumar
  • 395
  • 2
  • 4
  • 15
  • *"code to add all the data to file by reading sheet data"* `resolve` will only be getting called before that work is done if that code you've left out does its work asynchronously. (It can be hard to know for sure which code to include and which to leave out.) If so: https://stackoverflow.com/a/43766002/157247 – T.J. Crowder Apr 09 '18 at 11:29
  • Use async-await, await on the called method (async) so that the entire promise could be awaited on. – user2347763 Apr 09 '18 at 11:40

0 Answers0