0

update: clarified and edited code to reflect what I really want, that is, send a streaming response, that is, send back matched results as they arrive from their own async matching process.

consider (using expressjs-ish code)

app.post('/', jsonParser, function (req, res) {
    if (!req.body) return res.sendStatus(400)

    // matches is not needed for this scenario so
    // commenting it out
    // var matches = [];
    req.body.forEach(function(element, index) {

        foo.match(
            element, 
            function callback(error, result) {
                if (error) {
                    console.log(error); // on error 
                }
                else {
                    ⇒ if (some condition) {
                        // matches.push(result);
                        ⇒ res.send(result);
                    }
                }
            }
        );
    });

    // moved this above, inside the callback
    // ⇒ res.send(matches);

});

The input to post('/') is an array of terms. Each term is matched using foo which has a callback after every call. I want to send back all the matches that satisfy "some condition" (see in the code above). Ideally, it would be good to send back a streaming response, that is, send a response back as the matches occur (because foo.match() might take a while for each term). How do I go about this?

punkish
  • 13,598
  • 26
  • 66
  • 101
  • Your question is about the streaming or how to create an array that finally will send back to the client? – Stavros Zavrakas Oct 04 '16 at 21:28
  • updated question to clarify that I want to send a streaming response back, not the final array. Thanks for noticing that confusion. – punkish Oct 05 '16 at 07:05

1 Answers1

1

Does something like that works for you? I' ve used the stream-array module. Possibly this can be helpful for you? How to emit/pipe array values as a readable stream in node.js?

var streamify = require('stream-array');

app.post('/', jsonParser, function (req, res) {
  if (!req.body) {
    return res.sendStatus(400);
  }

  var matches = [];
  req.body.forEach(function (element, index) {
    foo.match(
      element,
      function callback(error, result) {
        if (error) {
          console.log(error); // on error 
        } else {
          if (some condition) {
            streamify([result]).pipe(res);
          }
        }
      }
    );
  });

  // res.json(req.body);
});
Community
  • 1
  • 1
Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
  • unfortunately it didn't work, though it could be something wrong on my end. Let me explain again. I don't want to wait to build the entire array of results. I want to send back the results as they arrive from `foo` which is an async operation. No need to store results in between, just send them as they arrive. I've voted up your answer because it could be me who hasn't implemented it correctly, but it does look promising. – punkish Oct 05 '16 at 15:18