I have the following code.
var queue = require('async').queue(function(task, callback) {
console.log("QUEUED JOB STARTED");
require('sleep').sleep(10);
console.log("QUEUED JOB FINISHED");
callback();
}, 1);
router.post('/generate/', function(request, result) {
console.log("REQUEST RECEIVED");
queue.push({});
result.end();
console.log("REQUEST PROCESSED");
});
When the first request comes it is immediately processed. But when the next comes just after the first one, the server waits until the queued job is finished (10 seconds) so the output is:
REQUEST RECEIVED
REQUEST PROCESSED
QUEUED JOB STARTED
QUEUED JOB FINISHED
REQUEST RECEIVED
REQUEST PROCESSED
QUEUED JOB STARTED
QUEUED JOB FINISHED
Is it possible to make Node.js enqueue and answer the second request without waiting for the first to achieve the following output?
REQUEST RECEIVED
REQUEST PROCESSED
REQUEST RECEIVED
REQUEST PROCESSED
QUEUED JOB STARTED
QUEUED JOB FINISHED
QUEUED JOB STARTED
QUEUED JOB FINISHED