Dealing with errors while processing multiple asynchronous requests simultaneously

I am dealing with 6 asynchronous requests. The problem I'm facing is that if one of them encounters an error and returns a 404 status code, the other requests stop functioning as well. I have implemented the use of async.parallel to handle these requests, but unfortunately, I haven't been able to get it to work properly.

Below is the snippet of my code:

    async.parallel({
      request1: async (callback) => {
        const [err, result] = await to(this.$store.dispatch('myAction1', {
          id: this.$route.params.id,
        }));
        callback(err, result);
      },
      // Code for request2 to request6 omitted for brevity
      
    }, (err, results) => {
      if (err) {
        // Display an error message when any request fails
      }
      // Perform actions when all requests are successful and hide the page loader.
      this.hidePageLoader();
    });

The issue is that even if one of the requests returns a 404 error, the page loader still shows up. My goal is to either pass a failed request as null in the results object or return the other results without including the failed request in the results object. How can I achieve this correctly?

Answer №1

To ensure that the remaining tasks can continue processing even if one of them fails, it is important to store the tasks in an array where each task is wrapped with async.reflect. Then, set this array of tasks as the first argument for async.parallel.

Here's an example:

async.parallel([
 async.reflect((callback) => {
   return callback(null, "one");
 }),
 async.reflect((callback) => {
   return callback("error");
 }),
 // additional tasks
], function(error, results) {
  if(error)
    return cb(error)

  // results[0].value ->> "one"
  // results[1].error ->> "error"

  return cb(results)
})

Source: Async Documentation utils >> reflect

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Angular UI Bootstrap Accordion currently has the first accordion group closed instead of opened

I am currently utilizing UI-Bootstrap Accordion to populate content from a JSON Object. My goal is to have the first accordion group open by default upon loading, but unfortunately, this feature is not working as expected. Although I have set is-open to t ...

Make the active tab stand out in the navigation menu

Could you please advise on how to highlight the active tab in a menu when users switch to another page? This is my current code: .nav { float: left; font-size: 125%; } .nav ul { margin: 0; } .nav li { background: none repeat scroll 0 0 #77 ...

encountering issues with passing intricate data through Ajax to a controller in ASP.NET MVC, leading to the object returning null

I'm encountering an issue where the object returns null when posting complex data to a controller in asp.net mvc using ajax Here's the code snippet that is causing the null return: //Ajax Call $.ajax({ type: "POST", url: ...

The function initiates without delay upon meeting the specified condition

I am looking to trigger a function automatically upon certain dynamically changing conditions in my JavaScript code. I attempted using the document.body.onload method, but it did not work as expected. document.body.onload = myFunction() function myFunct ...

Encounter: Unable to lock session. The session_write_close() function is not functioning as intended

The concept of session locks has been a widely discussed topic, and I am familiar with the use of the "magic" session_write_close() function. However... I have an Ajax request that executes a lengthy operation (taking several minutes). To track progress, ...

What is the best way to showcase a list of divs in three columns rows using a forEach loop?

I need help troubleshooting my code. I'm trying to display an array called posts:[], which consists of a JSON object. Everything seems to be working fine except for the fact that I can't get three columns to display in a row using Bootstrap. I ha ...

Loading Webfonts Asynchronously in NextJS Using Webfontloader

I am currently working on a NextJS app where I am using webfontloader to load fonts dynamically. function load() { const WebFont = require("webfontloader"); WebFont.load({ google: { families: fonts } }); } However, I ha ...

Generate some text on the following page utilizing the JavaScript function window.print()

Within my ASP.NET MVC View, I included this code snippet to display the content of my webpage: var newWindow = window.open(); newWindow.document.write(); newWindow.document.write(document.getElementById("sbmtform").innerHTML); newWindow.print(); I also ...

Submitting forms with Ajax in IE(8)

Sample Google form Related spreadsheet I modified the original code to create two custom forms: First created form Second created form Both forms are functional on most browsers except for IE(8). Any idea why? First form: <!DOCTYPE html> <h ...

Is there a regular expression that can match any permutation of three digits?

Is it possible to create a regular expression that can determine if a string contains at least 3 digits, regardless of their order? (I would appreciate seeing the exact regex solution for this.) For example: abk2d5k6 //3 digits abk25k6d //also has 3 dig ...

`How to resolve the error: "Unhandled SyntaxError: Unexpected character _ found in JSON at position . . . "`

While attempting to output a JSON encode within a PHP code, I encountered an error in the network tab of Google Chrome. This is my first time dealing with errors involving AJAX and JSON. Uncaught SyntaxError: Unexpected token _ in JSON at position 1 at ...

How can the entire row in an ngTable be turned into a clickable hyperlink, allowing it to open in a new tab or window, using AngularJS?

Currently, I am using AngularJS in conjunction with ngtable. Whenever I create a table, my usual practice is to include ui-sref on the <tr> element as shown below: <tr ng-repeat="element in $data" ui-sref="app.some.details.info({id: element.id}) ...

Is there a way to verify if a Backbone.View is actively displayed in the DOM?

Is there a way to determine if a Backbone.View is currently rendered in the DOM, so that I do not need to rerender it? Any suggestions on how this can be achieved? Thanks and regards ...

CSRF token not found during file upload via AJAX request

Encountering an issue with my ajax request in Django where it's reporting a missing CSRF token when trying to upload a file. I've compared it to other working ajax requests and suspect the problem lies in passing a file for upload. Getting a 403 ...

What are some methods to avoid redundant data across various windows?

In the process of developing an angular application, I have implemented a websocket for real-time updates transmission and reception. Within my application, users have the ability to open multiple windows that are launched from the main window, solely for ...

I developed a Stopwatch feature and now I am looking to showcase the lap times on my HTML page in a layout with 4 columns and 4 rows when the lap

Looking for a way to showcase the laps from your stopwatch in your HTML page? You're in luck! I have a solution that will display the laps in 4 columns. To see the complete code, check out this link: https://jsfiddle.net/waqasumer/agru2bdL/ Index.ht ...

What is the best way to retrieve a previously used jQuery function?

The dynamic table I've created has the functionality to search, display search results in pagination, and auto-compute fields. This is how it works: Users search for their items in the search box If the search results are extensive, the system displ ...

javascript detect when two div elements are overlapping

On my webpage, I have implemented the effect.shrink() function. However, when clicking quickly on the page, the div tags start overlapping with other elements. What is the best way to solve this issue? I am using both scriptaculous.js and prototype.js fo ...

Sorting data by percentages in AngularJS

I am currently facing an issue with sorting percentages in a table column. Despite using methods like parseFloat and other AngularJS (1.5.0) sorting techniques, the percentages are not being sorted as expected. [ {percentage: 8.82} {percentage: 0. ...

When setting up a firebaseApp, be sure to globally configure the region to europe-west1 for httpsCallable functions, rather than the default us-central1

I am looking for a way to globally set the region from which an httpsCallable function is being called. Currently, I am initializing my app in the following manner: const app = firebase.initializeApp(firebaseConfig) app.functions('europe-west1') ...