What causes the disparity in async.js when it comes to the divergence between waterfall and series/parallelLimit(1)?

This is the initial code that works fine:

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.waterfall([ //  <-- waterfall
        checkIfTheFileExists,
        readTheFile
    ], showTheFile);
}
testAsync() // "file was read"

The following doesn't seem to work. Only difference is the first uses async.parallel instead of async.series.

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.series([ //   <-- only line of code that differs.
        checkIfTheFileExists,
        readTheFile
    ], showTheFile);
}
testAsync() //     <-- no output is displayed on the console, not even a blank line.

The async.series version did not produce any output on the console. What could be causing this issue?

I also attempted an async.parallelLimit version with the limit set to 1. I expected it to execute the tasks in series due to the limit but again did not see any output on the console. Here is the async.parallelLimit version:

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.parallelLimit([  //    <--- this line is different
        checkIfTheFileExists,
        readTheFile
    ], 1, showTheFile); //   <--- and this line is different.  I added the "1".
}
testAsync() //           <--- no output on the console.

The file does exist in all three test cases.

Answer №1

Why is the async.series implementation not displaying any output on the console? What could be the reason for this lack of response?

It appears that there may be a misunderstanding regarding the nature of the async.series function. While it executes the tasks in sequence, similar to async.waterfall, each task operates independently without passing results between them. Therefore, the outcome of checkIfTheFileExists does not get passed to readTheFile. This differentiation should be considered when utilizing async.series.

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

Best practice for calling next() in node.js while fetching data with Firebase

I find myself struggling with a simple problem, often the answer is right in front of me but I still can't seem to figure it out. Currently, I am working on an app using node.js along with the express.js framework. For my database needs, I have incor ...

Acquire the URL using Angular within a local environment

I am currently working on a basic Angular project where I have a JSON file containing some data. [{ "name": "Little Collins", "area": "Bronx", "city": "New York", "coverImage": "https://images.unsplash.com/photo-1576808597967-93bd9aaa6bae?ixlib=rb-1.2.1&a ...

Implementing virtual hosts and HTTPS encryption

Is it possible to configure vhosts on Express with https? This is my current code without SSL: var express = require('express'); var vhost = require('vhost'); var path = require('path'); var appOne = express(); var appTwo = ...

The React syntax is malfunctioning

componentDidMount() { const restaurants = Restaurant.all() restaurants.then( rests => { this.setState({ restaurants: rests }) }) } render() { const { restauran ...

Having difficulty passing an object in a GraphQL mutation

I've been trying to figure out how to pass an array of objects in a GraphQL mutation from the React JS side. I attempted passing the object in React but encountered an error stating that it's not the same type. [input type][1] [from api side][2] ...

The issue of jQuery POST methods consistently failing

I have set up a Node.js server using Express with a fairly straightforward configuration: var express = require('express'); var app = express(); app.configure(function() { app.use(express.urlencoded()); app.use(express.json()); app ...

Access your Node.js application by using the address on your Laravel Forge server

My Node.js app is successfully running on Laravel Homestead, but I now need to transfer it to the server. I am using Laravel Forge for server provisioning and all the necessary Node packages are already installed. While testing locally, I use Homestead&ap ...

Resource loading error: The server returned a 404 (Not Found) status code, as shown in the console

Click here I have a simple file structure where index.html, script.js, and login.js are all in the same root folder without any subfolders. The issue I'm facing is that although the connection to the database works fine, there seems to be a problem wi ...

I'm encountering an issue where the command "node" is not found, even though the full path is working. How can I get the command "node -v" to work properly?

After recently installing node.js, I encountered an issue when trying to confirm the installation by running 'node -v' in my command shell. Instead of getting the version number, I received a "bash: node: command not found" error message. Searchi ...

What is the best way to retrieve a list of fields from a set of JSON documents?

Currently, I am facing a scenario where I have two MongoDB collections named stu_creds and stu_profile. My objective is to initially fetch all the student records from the stu_creds collection where the stu_pref_contact field corresponds to email. Subseque ...

I am looking to manage user-related data in the comment model using mongoose and express

I have a user, post, and comment modal This is my comment modal import mongoose from "mongoose"; const CommentSchema = new mongoose.Schema({ postId: { type: mongoose.Schema.Types.ObjectId, ref: "Post", }, userId: { t ...

Node.js for Streaming Videos

I am currently working on streaming video using node.js and I recently came across this helpful article. The setup works perfectly when streaming from a local source, but now I need to stream the video from a web source. However, my specific requirement i ...

How to install express using Terminal on a Mac operating system

Need some help with this code. Running it on my Mac with npm installed as an Admin, but still getting errors. I've been searching for a solution online for hours. Matts-MacBook-Pro-3:Start Matt$ npm install express-generator -g npm WARN checkPermissi ...

Create efficient images using Node.js and express using sharp or canvas

Struggling with optimizing image rendering using node, express, and sharp. Successfully implemented an upload method with Jimp for images over 2000px wide and larger than 2mb in file size. While many libraries can achieve this, Jimp was more memory-effici ...

Arrange outcomes according to a category in a separate table

My database consists of two tables - 'products' and 'variants.' The 'variants' table has a field 'product_id' linking to a product in the 'products' table, along with a 'price' field. I am utiliz ...

I am encountering a consistent issue with receiving bad requests while trying to execute a POST request with axios. I am unsure of the root cause of this problem and

Issue with axios post request throwing BadRequestError I encountered this error message while trying to send a post request using axios. Here is the code snippet where I gather input data from a form and make a post request to my login URL with the obtain ...

What is the best way to incorporate the data returned from a controller into EJS data?

Currently, I am working on a Node.js application where I am trying to fetch a list of clients using the latest versions of Express and Passport. My goal is to retrieve the return data from a controller method and pass it through EJS to display in the view ...

I am interested in subscribing to a topic on an MQTT broker with Azure IoT Hub. My goal is to have the data stored in Azure IoT Hub whenever I publish my topic

Recently, I delved into the world of MQTT and am currently in the process of configuring a MQTT protocol to transmit data from gateway devices to Azure IoT Hub. However, I am encountering an issue - I can't figure out the best method to receive and st ...

Issue encountered while attempting to launch a Docker node container on Windows 10 system

As a beginner in the world of Docker, I decided to experiment with running simple examples on both my Windows 10 PC and Mac simultaneously. Interestingly, the example runs smoothly on my Mac but encounters an issue on the Windows machine. After setting up ...

Unfortunately, bower-rails is no longer able to install any packages

For some unknown reason, a current project installed with bower-rails cannot perform bower:install anymore! Even though it was working previously and I already have packages installed locally, now I am experiencing issues. No changes have been made! I at ...