Answer the question
In order to leave comments, you need to log in
Is it correct to wrap the entire script in async?
Hello
Recently I learned what async / await is and what they eat with.
It turned out to be very tasty.
But, the question arose:
Would it be correct/logical to wrap the whole script in (anonymous-async-function() {...})() to use inside await without restrictions?
Intuition tells you not to do it.
Is it so? Why? In which cases?
Answer the question
In order to leave comments, you need to log in
You can do it, but outside the anonymous function the code will be synchronous. If we are talking about wrapping the entire script and you have 1 file with 200-300 lines, then it is quite normal, as for me. If you want to wrap an angular application, then my instinct tells me that you will get confused and / or there will be some problems with imports (did not check). Moreover, you need to catch reject inside such an anonymous function via try-catch at the top level.
But, as for me, async should be used only where it really simplifies the work, promises are quite suitable for many things, and somewhere callbacks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question