r/Web_Development • u/latest_ali • Dec 12 '20
I have a problem understanding execution flow in js. Is there a way I can run js line by line in VS code so I have a better understanding of how the execution flow work for async and promises code?
I am having a hard time grasping how the flow of js script work. Imagine I have a async function and I have a await statement after that and I am return the result data. after this line I am using this result in the next following line so I am not sure when they say `the function goes into event queue until it will get the result and the control will be back to when it is called` mean.
Same with promises. if I have a function and I call a method that return a promise and I run .then on the result (all in a function like this `const outer = ()=> return x.get().then((data)=>data.json()))`) and I let `dataToChange = outer()` in my code and I use that data in different part of the script in the following line how the execution follow through?
Alternatively is there a way I can execute JS line by line to see what happens after await and .then?
2
u/tetractys_gnosys Dec 12 '20
Haven't played with this in a while but try out a VS Code extension called Quokka.
2
u/Friarchuck Dec 12 '20
Yes the debugger works the same way it does in the browser. Set a breakpoint and you can step through.
2
u/caseydwayne Dec 12 '20
personally I'm a big fan of inline debuggers and use them constantly on big projects (enabled with a local value or configuration setting). this way you can set whatever you need to report out and save yourself a lot of unnecessary issues.
Also, I'd use async / await for modern js -- it's largely the same and uses a much cleaner approach.