简体   繁体   中英

Co - generator based lib for node.js. How i can run parallel tasks?

I have tried generator based async lib co ( GitHub ) for node.js

Here is the code. I use co-express and co-wait.

As you can see client wait 10 seconds before he get response. My problem is - if i try to run multiple requests to this url and all functions will block next calls.

How i can run multiple calls to this url in parallel? localhost:8000/test

var fs = require('fs');
var co = require('co');
var express = require('express');
var wrapper = require('co-express');
var app = wrapper(express());
var wait = require('co-wait');

app.get('/test', function* (req, res, next) {
    yield waitAndAnswer(res)
});

function* waitAndAnswer (res) {
  yield wait(10000);
  res.send('Done: ' + Date.now());
}

app.listen(8000);

I made a mistake, when tried to call this url by single browser. Looks like it standard behavior of browsers, so this code work as expected. Jonathan Ong have answered my question.

if you're making calls via your browser, then its a browser thing. if you call it via curl or something, it should be fine. – Jonathan Ong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM