Node.js: serial operations with branching
NickName:Sean Lynch Ask DateTime:2015-09-22T02:11:49

Node.js: serial operations with branching

I'm new to node.js and using it for a backend that takes data from syslog messages and stores it to a database.

I've run into the following type of serial operations:

1. Query the database

2. If the query value is X do A. otherwise do B.
    A. 1. Store "this" in DB
       2. Query the database again
       3. If the query value is Y do P. otherwise do Q.
           P. Store "something"
           Q. Store "something else"

    B. 1. Store "that" in the DB
       2. Store "the other thing" in the DB

The gist here is that I have some operations that need to happen in order, but there is branching logic in the order.

I end up in callback hell (I didn't know what that is when I came to Node.. I do now).

I have used the async library for things that are more straight forward - like doing things in order with async.forEachOfSeries or async.queue. But I don't think there's a way to use that if things have to happen in order but there is branching.

Is there a way to handle this that doesn't lead to callback hell?

Copyright Notice:Content Author:「Sean Lynch」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32701949/node-js-serial-operations-with-branching

More about “Node.js: serial operations with branching” related questions

Node.js: serial operations with branching

I'm new to node.js and using it for a backend that takes data from syslog messages and stores it to a database. I've run into the following type of serial operations: 1. Query the database 2. If...

Show Detail

node.js and Serial ports

I'm trying to build a simple web app that reads the clients' serial port that is connected to a barcode scanner. I have done this using ActiveX but it only works on IE browsers, i have heard that I...

Show Detail

How is SVN v 1.8 branching / merging compared to Git?

I have been using Git for a while now, and have been recently asked why its merging and branching capabilities are better than those of SVN. Years ago, when I used SVN, I found that branching and m...

Show Detail

Design patterns for a serial of operations then commit

Saying I have a serial operations to update the data of a UI, like add(), delete(), update(), clear(). Those operations can be combined and permuted to show specific result. And notice that after a...

Show Detail

switch vs if-else branching control structure in Node.JS

Which one is good to use when there is a large number of branching flow in Node.JS Program. switch switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; defa...

Show Detail

Handle Async Operations in Node.js

I have been using the async package from NPM inside my Node.js service to run different operations in sequence. Example: when I would like to run functionA, functionB, functionC in sequence. I woul...

Show Detail

Serial OperationQueue with Operations synchronizing timer and sleep

I have a serial OperationQueue whose operations call usleep. I do this because the operation execution block synchronizes with a Timer that needs to repeat until a designated time. For example, 3

Show Detail

Is the preference with SIMD to always get rid of branching?

If you are writing some SIMD code that will be run by another program, is it always favorable to get rid of branching to increase performance? I heard that even doing extra operations just to avoid...

Show Detail

node.js for cpu intensive operations

There is something that I'm really not understanding about Node.js: pretty much everywhere you can read that node.js is not recommended for HPC (high performance computing) due to his async but sin...

Show Detail

TCP, UDP, and serial communications using a single Node.js server

We're developing a controller for a test fixture that needs to communicate via a serial port and UDP. My plan was to use Node.js to provide the I/O communications hub as shown in the figure below. ...

Show Detail