How To Return a Response From Asynchronous Calls in JavaScript

How To Return a Response From Asynchronous Calls in JavaScript

Leverage promises in your asynchronous functions .How To Return a Response From Asynchronous Calls in JavaScript

People who are new to asynchronous programming are often confused by the following situation:

function myFunction() {
      ajaxCall(..., function onSuccess(result) {
        // somehow return result from myFunction

    // should print the result of the ajax call.
    console.log('result of myFunction', myFunction()); 

While this example shows a specific use case for an AJAX call, it represents a much more general pattern: synchronously returning the result of an asynchronous function. Though transforming an asynchronous call into a synchronous one is not possible in JavaScript, I will show you how to solve your problem without destroying the idea behind asynchronous programming.

Types of Asynchronous Functions

Before we jump to a conclusion, we should take a quick look at the most common types of asynchronous functions. They differ in the way they provide their asynchronously gathered value (or simply how they finish whatever they are supposed to do). There are three prominent ways of doing this:

1. Callbacks

A function might take a callback function as a parameter and will call this function with the demanded value whenever it is finished. An example of this is the previously shown ajaxCall function that takes a callback function as its first parameter. This callback function is called whenever ajaxCall is finished with its HTTP request. Further, that callback function takes the result of the request as its first parameter. That is how you get the value.

2. Promises

Promises came with ECMAScript 6 (ES2015). A promise is an object that is immediately (synchronously) returned from an asynchronous function and enables you to track the state of that function. A promise can be in one of the following states:

  • pending: (initial) neither fulfilled nor rejected
  • fulfilled: The operation was completed successfully.
  • rejected: The operation failed.

Promises deserve their own article. So if you want to know more, check out the Promise API

programming javascript react

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

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Increase Performance of React Applications Via Array JavaScript Methods

Increase Performance of React Applications Via Array JavaScript Methods. We will create a simple event management application in the react to add, update, and delete an event.

Qual a relação entre Java e JavaScript, C e C++, React e React Native

Qual a relação entre Java e JavaScript, C e C++, React e React Native

How to use Functional Programming in your React app | React Next

In the past few years, the buzz around functional programming has been growing, but it can be challenging to apply theoretical concepts to everyday work. How can we make the code we work on more functional? What advanced patterns can you use, and why should you do that? And what do React hooks have to do with all this? In this talk we’ll have a look at real-life examples and patterns you can use to make React apps more functional.

JavaScript Date: Building a React Hook For Date Selector

This week, I had to construct a date selector form in React that looks like this. As you can see, the user interface includes the surrounding month and date options in addition to the selected date. This means that I need to not only keep track of the current date, but I also need a way to conveniently figure out how many days are in the selected month.