S
S
Sergey Sergeev2014-12-11 19:26:43
Node.js
Sergey Sergeev, 2014-12-11 19:26:43

How to make async.js and express.js friends?

Help me figure it out
There is such a code, the point is that you first need to make a series of parallel requests and then display the callback on the screen in the browser. The code below doesn't work.

async.parallel([
    function(){ ... },
    function(){ ... }
], 
  function(){ 
    app.get('/', function () {
        res.send('test');
    })
 }
);

If you do it like this, then everything works. Only the result is returned to the browser before the first two requests are completed. What's my mistake?
async.parallel([
    function(){ ... },
    function(){ ... }
], 
  function(){ 

 }
);
    app.get('/', function () {
        res.send('test');
    })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sergeev, 2014-12-11
@alokazay

mayorovp - yes, logically, everything is right, I did it right away. But that didn't work either...

app.get('/', function (req, res) {
    async.parallel([
        function () {
            ...
        },
        function () {
            ...
        },

    ],function () {
        res.send('test');
    });

})

M
mayorovp, 2014-12-11
@mayorovp

Er... think again what you're doing. The first time you first execute your requests - and only then do you start listening for connections from the browser at all! Of course, you can't do anything.
The second time you wrote two parallel processes - one executes a bunch of requests, the second - listens to a request from the browser.
You must first receive a request from the browser - that is, app.get should be the first line. Then you need to make several requests - that is, there must be a call to async.parallel inside. And only at the very end should there be a call to res.send. I think now you yourself will be able to properly assemble this matryoshka.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question