Stop execution of an asynchronous function

Asked

Viewed 126 times

1

Hello, I’m a little new in the Javascript universe and its asynchronous. I have the following asynchronous function below. When entering the condition "!= null", I wish it to be terminated and the following functions will not be performed. However, a "Return" is simply not working.

async function register() {
            await checkExists(id)
                .then(string => {
                    if(string != null) {
                        // return - parar a execução do resto aqui;
                    }
                })

            await getString(id)
                .then(string => {
                    // Próxima função...
                })
}

Can someone help me?

1 answer

0

I think there is a small lack of detail in the understanding of async functions, we cannot stop an async Function because it has already been added to the execution chain by the Event loop when the interpretation.

That means you can’t stop this loop but you can ask to quit the process of your server with process.exit()

But this solution will cause you other problems like leaks or state errors, try to research about generators and about Yield.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.