Calling async function in main file

Viewed 25308

I am creating App integrating two systems. Therefore, I am using some requests and async functions. It's no problem to call async function in async function. However, I need to end somehow this chain and call async function in my main file where is App served from. Do you have any idea how to do it? Part of code looks like this

async function asyncFunctionINeedToCall() {
  await childAsyncFunction()
}

asyncFunctionINeedToCall()

2 Answers

Since the main scope is not async, you will need to do an async anonymous function that calls your function and itself :

(async function() {
  await yourFunction();
})();

Or resolve the promise :

yourFunction().then(result => {
  // ...
}).catch(error => {
  // if you have an error
})

For anyone looking for an arrow function version of the async function that calls itself, here it is:

(async () => {
  await callAsyncFunction();
})();
Related