1

I have two time taking functions -> getExcelFileStream and loadWorkbook

I want the function structure to return a response(pass/failure) only after both these subfunctions have been executed. But it always returns a promise pending clause.

async function parseToJson(data) {

    const excelFileStream = await getExcelFileStream(data.url);
    const dataRows = await loadWorkbook(excelFileStream, data.orgId);

    return dataRows;

}

exports.uploadExcel = function uploadExcel(data) {
    return parseToJson(data);
};

algo_user
  • 196
  • 8

2 Answers2

0

According to the doc (Syntax > Return value)

A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.

So it should be

exports.uploadExcel = async function uploadExcel(data) {
  return await parseToJson(data)
}
hgb123
  • 13,869
  • 3
  • 20
  • 38
0

It is not possible to make an asynchronous code synchronous in javascript, and with javascript being single threaded, you can't write code to wait for the promise without blokcing the execution of the code handling the promise itself.

I am guessing when you're calling your function uploadExcel, you don't have any await or a .then() so you get a promise instead of the value of the resolved promise.

Unfortunately, You only have two options:
1- Let async/await propagates all the way up in your code
2- Make all your code synchronous, and that would be a very bad idea.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53