Skip to main content

Node.js Looping and Async Calls: A Love Story

Problem

I needed to create a node application that would ping a list of servers and return their status (if are they up or down).  All seemed easy enough until I needed to create asynchronous calls within a “for loop”.  The async method’s callback contained logic to simply build and return another object with the result data. Unfortunately, I couldn’t get ANY data back.

Research

After poking around some blogs and chatting with a coworker, I learned about node’s infamous “queue” of processes which fires AFTER my code finishes execution.  That is, when my “for loop” makes the async calls (let’s say, 5 times), the callback doesn’t fire until the end of my code is reached. Here’s what I initially coded:


“returnObj” was serialized and written to the DOM well before the async callback was executed, writing a big, fat NOTHING to the browser.  This confused me for hours. What I needed to do was somehow write the response to the browser AFTER I get the data from the callback.

Solution

This was another problem a recursion method could resolve.  It’s probably not the most efficient, but it was fast enough for me. Here’s the code:




I created a function called “probeit” accepting an array of host names, the node response object, and a json object to return. I loaded the array with host name before calling this function, and passed empty objects that will be loaded during the recursion (res and returnObj).

Think of this as a process of elimination.  We pass the hosts array, we splice the first value, make an async call, and call the function within itself over and over again until the array is empty.  When the array is empty, then stringify and write the object to the browser. WORKED!!

Hope this helps somebody.


Comments

Popular posts from this blog

Resolved Sitecore "If you publish now, the selected version will not be visible on the web site" warning

The Problem:   Unable to publish any Sitecore item within a particular site, even out of the workflow. Rather, a warning reads " If you publish now, the selected version will not be visible on the web site " What I've Discovered: I couldn't publish any item in the site, not just one or two I viewed all parent items of the items in question The main home page displayed a different warning " This item will never be published because it's publishable option is disabled " Another sitecore developer reminded me of the standard fields option View --> check standard fields to show all standard page fields Found out that somebody checked Never Publish within the publishing section The Fix: After I unchecked the checkbox and saved the change, I was able to publish again.  :) Conclusion: Looks like another Sitecore user thought the children items would not be affected by this change.  Lesson  learned.

[Resolved] Sitecore ParseException: End of string expected at position...

Problem:  I have a line of code that uses Sitecore Fast Query to pull all items + children starting with a site item, like so: Item [] allItems = db.SelectItems( "fast:" + sitecorePath + "//*" ); Unfortunately, I would get a Sitecore parsing error at runtime: ParseException: End of string expected at position... Turns out Sitecore doesn't like hyphens ('-') in any sitecore path when using fast query, which I have a few distributor sites in a folder which contained hyphens. Solution: I create a simple method that resolves a sitecore path to be Sitecore fast query friendly:             string sitecorePath = "" ;             if (siteItem.Paths.FullPath.Contains( "-" ))             {                 String [...

Create a File Upload Service using Node.js / Express / Multer

Problem:    I needed to create a form that essentially uploads a file to a server, then posts the rest of the form data to a second server with the new file name. In other words, post the image to an “Image” server, then posts the form data (with the file URL) to a “Form Data” server. Solution:  Create an Express server that uses the “Multer” library to accomplish this. The Code: Create the express server and have it listen on a specific port var express = require( 'express' ); var app = express (); app . listen ( 3313 , function (){     console . log ( 'listening on port 3313' ); }); Install Multer.  Read here: https://www.npmjs.com/package/multer Include Multer to your express app.  We’ll be tweaking the storage properties so you can name the file whatever you like. At the end, you should have an “upload” object you’ll be using in the next step. be sure to change the file destination of your choice. var expre...