Javascript Promises

Let us dive a little deeper into promises and asynchronous programming. If you already understand how asynchronous programming works in javascript, you can jump to the promises section. However if you want to under the history behind why promises exist, then read on. Javascript Promises are a much needed feature to have. They address a problem known as callback hell, which one is bound to face if they do any kind of asynchronous programming. As a web developer, this is something you cannot run away from as the browsers internal operations are asynchronous. Asynchronous here means more than one thing is happening at a time within a browsers engine. You might also hear the term synchronous in a similar manner. It means one thing happening at a time. For example, you may have heard that javascript is synchronous. This means that when executing javascript you cannot execute multiple statements at a time. Every statement that begins to execute MUST complete before another is run. This is why you hear that javascript is single threaded. Code execution is run line by line.

Other languages like Java have the ability to run multiple commands at a same time by delegating them to a different memory on the system (known as multi threading). By default this asynchronous functionality comes out of the box with the language. So how does javascript do asynchronous programming? Let me try clarify that here. You see, within a browser, many things are happening. There are many engines running simultaneously within it. The javascript engine is one of them. Another might be the rendering engine, which displays HTML document and images. The browser also has networking capabilities. So when you decide to make HTTP requests, it uses this component found in the browser to make that request. Check out how the browser works here. Ok, so now u see that everything going in the browser is asynchronous. But within the javascript engine itself, everything is synchronous. Read More