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);
        });
 
    