A Gentle Introduction to JavaScript Promises. I Promise :).

A Gentle Introduction to JavaScript Promises. I Promise :).

Promises are a fairly new API in JavaScript so we will first look at how asynchronous operations were done before promises. The most common way was to pass in callback functions that were called and ran after the success or failure of the asynchronous function.

Asynchronous operations before promises.

Sometimes you have a function or operation that takes a considerable amount of time to finish like a network/service request to fetch some data or resource but then you want to continue executing the rest of your program and then use the data returned from the operation whenever the process completes.

Instead of waiting on the call to complete you can let the rest of your program that doesn’t rely on the data from the operation to continue running and only run the dependent code after the operation returns a value.

Promises are a fairly new API in JavaScript so we will first look at how asynchronous operations were done before promises. The most common way was to pass in callback functions that were called and ran after the success or failure of the asynchronous function.

We will use a simple social media application page where a user’s basic information and list of their friends are displayed as an example. To get all the data needed to display the page we might need to call multiple services but we might not be able to call all of them simultaneously as some calls will likely depend on data from another service.

In the case of our imaginary social media app we might need to call the login service first and then call the user information service once the user’s credentials are validated. If the login is successful then we might want to use the user’s id to fetch their friends list to display on the page along with other data.

Here is some code we might use to get the data we need to render the page. Some code has been omitted for brevity.

function successCallback(userData) {
    // display user's basic information and a list of their friends        // if the operation completed successfully
}

function errorCallback(errorMessage) {
    // display the error message.
}
function login(successCallback, errorCallback, userName, password) {
    // do some operation here then run the callback function
    // call the user service
    callUserInfoService(userName, password, function(user, error) {
        if(user) {
            // get the user's friends
             getUserFriends(user, function(friends, error) {
                 // call the friends service
                 if(friends) {
                     successCallback({
                         friends : friends
                     });
                 } else {
                      errorCallback(error);
                 }
            });
        } else {
            errorCallback(error);
        }
    });
}
login(successCallback, errorCallback, userName, password);

software-engineering software-development javascript javascript-tips promises

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Wrestling Betting Software Development | WWE Betting Software Developers

Wrestling betting software development solutions from expert Wrestling betting app and software development company delivers ultimate betting experience. Chat with us.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company