![]() JavaScript Promises allow you to avoid blocking code. For these 5 seconds, the site will not be changing the background color. ![]() In a single-threaded application, you would have to wait 5 seconds for the API to respond, then resume changing the site’s background color. You have some other JavaScript code that should constantly change the webpage’s background color. At the same time, you still require other parts of your code to be executed.įor example, say you are making an API call that takes about 5 seconds to come back with a response. Every so often, you may want to accept user input, wait for an event to be triggered, or make an API call. JavaScript Promises are unique objects that define a function to execute. ![]() It is a one-liner! But if you are unfamiliar with async functions, you might need an explainer. Demo of the sleep function in NodeJS (also works in the browser)
0 Comments
Leave a Reply. |