Skip to main content

Posts

Showing posts with the label javascript

Return the response from an asynchronous call

Embrace the asynchronous behavior of JavaScript! While some asynchronous actions offer synchronous equivalents (so does "Ajax"), it's usually discouraged to use them, particularly in a browser context. Why is it not good do you ask? JavaScript goes in the UI thread of the browser and any long running process will lock the UI, creating it unresponsive. Moreover, there is an upper limit on the performance time for JavaScript and the browser will ask the user whether to continue the implementation or not. All of this is really not good user experience. The user won't be able to tell whether all is working fine or not. Also, the effect will be not good for users with a slow connection. In the following we will look at three different answers that are all building on top of each other: Promises with async/await (ES2017+, offered in older browsers if you use a transpiler or regenerator) Callbacks (popular in node) Promises with then() (ES2015+, of