Решение вызывается до завершения других функций. Как сделать так, чтобы решение вызывалось после выполнения других операций

У меня есть функция, где он будет читать документ Excel и записывать содержимое в файл. Как только запись будет завершена, я вызову функцию resolver, и функция вызывающего абонента загрузит файл пользователю.

Но решается вызов перед завершением других функций, в результате файл будет пустым при загрузке.

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

0 ответов

Другие вопросы по тегам