If node.js is the primary usage for programming for yall boys and girls, you should have known by now that it is asynchronous.
Now, what is asynchronous?
Well, there are two types of nature synchronous and asynchronous.
Synchronous is where code executes sequentially in order of the first line execution and then proceeds to the second line.
Asynchronous happens all at the same time simultaneously without caring about the state of the previous line of code.
Now, this can be a problem.
In playwright, we should be keen on using await for every code step.
In an asynchronous code, if the execution happens at the 100th line of execution, we will receive the output of the 100th line alone.
- Using the async() method, we are letting nodejs know that this is an asynchronous block set, and await is used within this block until every line of code is resolved sequentially instead of simultaneously.
- The first line waits and finishes before moving on to the following line.
- Now let us try to run this console log and see how it prints.
- The statement console.log("0") and console.log("========") are both outside the async function block so it wont wait till the previous set of codes completely executes.
- So when there is a time gap between codes, it executes the statements asynchronously.
So to avoid this mess, it is suggested that in playwright, we should use the async and wait for function before every method for the sequential flow of the code.