Wait for the event to happen before sending the HTTP response to NodeJS?

I am looking for a solution to wait for an event before sending an HTTP response.

Use case

  • The idea is that I call a function on one of my routes: zwave.connect("/dev/ttyACM5");This function returns immediately.
  • But there are 2 events that indicate that they will succeed or fail to connect the device:
zwave.on('driver ready', function(){...});
zwave.on('driver failed', function(){...});
  1. In my route, I would like to know if the device was successful or failed to connect before sending an HTTP response.

My decision"

  • When an event occurs, I save the event in the database:
zwave.on('driver ready', function(){
    //In the database, save the fact the event happened, here it event "CONNECTED"
});
  1. In my route, execute the connection function and wait for the event in the database:
router.get('/', function(request, response, next) {     
    zwave.connect("/dev/ttyACM5");
    waitForEvent("CONNECTED", 5, null, function(){
        response.redirect(/connected);
    });
});

// The function use to wait for the event
waitForEvent: function(eventType, nbCallMax, nbCall, callback){
    if(nbCall == null) nbCall = 1;
    if(nbCallMax == null) nbCallMax = 1;

    // Looking for event to happen (return true if event happened, false otherwise
    event = findEventInDataBase(eventType);

    if(event){
        waitForEvent(eventType, nbCallMax, nbCall, callback);
    }else{
        setTimeout(waitForEvent(eventType, callback, nbCallMax, (nbCall+1)), 1500);
    }
}

, , . , / ?

+4
3

asynchronous - , . ( , ES6, ES5.)

TL; DR

JavaScript (. : node.js?). , , Promise JavaScript (aka RxJS).

Promise

MDN:

Promise . A Promise , , , .

- , . connect Promise, :

const p = new Promise((resolve) => {
    // This assumes that the events are mutually exclusive
    zwave.connect('/dev/ttyACM5');
    zwave.on('driver ready', () => resolve(true));
    zwave.on('driver failed', () => resolve(false));
});

Promise, , "" :

// Inside your route file
const p = /* ... */;
router.get('/', function(request, response, next) {
    p.then(successful => {
        if (successful) {
            response.redirect('/connected');
        }
        else {
            response.redirect('/failure');
        }
    });
});

Promises MDN (, Promises).

+6

npm. .

, node , . .

   /*require sync module*/
var Sync = require('sync');
    app.get('/',function(req,res,next){
      story.find().exec(function(err,data){
        var sync_function_data = find_user.sync(null, {name: "sanjeev"});
          res.send({story:data,user:sync_function_data});
        });
    });


    /*****sync function defined here *******/
    function find_user(req_json, callback) {
        process.nextTick(function () {

            users.find(req_json,function (err,data)
            {
                if (!err) {
                    callback(null, data);
                } else {
                    callback(null, err);
                }
            });
        });
    }

: https://www.npmjs.com/package/sync

+3

? , zwave, , EventEmmiter,

router.get('/', function(request, response, next) {     
    zwave.connect("/dev/ttyACM5");
    zwave.once('driver ready', function(){
        response.redirect(/connected);
    });
});
+2
source

Source: https://habr.com/ru/post/1664108/


All Articles