Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling async function in main file

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()
like image 954
Tripo Avatar asked Oct 06 '17 08:10

Tripo


People also ask

How do you call async main function in Python?

To run an async function (coroutine) you have to call it using an Event Loop. Event Loops: You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Example 1: Event Loop example to run async Function to run a single async function: Python3.

What happens when you call an async function?

In this article The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the Await operator to the result. The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete.


1 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 }) 
like image 97
boehm_s Avatar answered Sep 23 '22 07:09

boehm_s