async javascript
could someone help me understand anything and everything about asynchronous js
5 Replies
TL; DR is that asynchronous JS doesn't block the event loop
In slightly more specific terms, event listeners are a great way to do that. Wait for something to happen then Do A Thing™️
After that, there's promises that are active async. You tell the computer to Do A Thing™️ and it'll do it, but it's gonna take some time so you don't want to block the event loop so you wrap it in a promise. The code will keep doing other things until the promise is fulfilled (either resolved or rejected). Whatever is in the
.then()
or .catch()
will then be run.
Async/await is pretty much the same thing, but it looks more like synchronous code. You mark a function as async
and the code knows that "this could take a while, when you hit await
don't wait, keep doing other stuff until the await
thing gets back to you".
Then, once the await
ed on thing returns, the code continues that async functionYou can learn a lot about it here:
https://javascript.info/async
lifesaver thanks
JavaScript.info is one of the best places to learn modern JS, so if you're just starting your JS journey it's a wonderful place to start
k ty