Building a JavaScript Asynchronous programming is a core concept in JavaScript and is a feature that gives JavaScript a lot of speed compared to other scripted programming languages. JavaScript is single-threaded which means it executes programs line-by-line. It is also asynchronous which means that if our program execution reaches a block of code that must wait on a result, it will continue on past this block of code that is waiting so the program doesn't freeze execution, and once that async task finishes, our code will handle the result it was waiting for by using a promise or a callback. Since the release of ES2015, the most common way to handle the result of an async function call is through a In this lesson, you will learn JavaScript promises by building a JavaScript promise implementation from scratch. Your promise will be declared exactly like they are in native JavaScript
Create a Implement a PromiseNOTE: If you want to see our promise implementation working with a real API request, check out the from the solution video. The JavaScript promise example we are building is meant to help you understand the fundamentals of promises and asynchronous thinking — it is not intended to show the most optimized version of a promise. I will first describe promises for beginners and then dive into our own promise implementation so we can understand it from the ground up. You've probably seen something like this before: The block of code in the Let me reiterate (because this is something that was difficult for me to grasp when I first learned promises), a To be able to wait on the server's response and execute the code in the The Now let's take a closer look at how the
I've named it When you create a promise, you do so like this The constructor also creates a When a developer calls Notice that the
When your asynchronous function calls This loop is wrapped in a NOTE: If you want to see our promise implementation working with a real API request, check out the from the solution video. Putting it all together with a more practical example: async/awaitThe |