Asynchronous 我怎样才能避免;回调已被称为";使用async.queue?

Asynchronous 我怎样才能避免;回调已被称为";使用async.queue?,asynchronous,callback,queue,asynccallback,Asynchronous,Callback,Queue,Asynccallback,我尝试使用async.queue进行一系列调用,每个调用都有自己的回调,我使用mocha测试是否返回了预期的结果 当我使用并发值1(我的线程数变量)时,这一切都非常有效。但是,当我使用任何大于1的值时,我会收到错误,说明“Error:Callback已经被调用了”。例如,如果我发送10条消息,并将我的线程数设置为5,前5条消息将顺利进行,但随后我开始在消息6或7周围看到重复的回调错误(见下文)。你知道我怎样才能避免这个错误吗 我的测试文件(其中定义了异步队列): 我在控制台中的结果: Sendi

我尝试使用async.queue进行一系列调用,每个调用都有自己的回调,我使用mocha测试是否返回了预期的结果

当我使用并发值1(我的线程数变量)时,这一切都非常有效。但是,当我使用任何大于1的值时,我会收到错误,说明“Error:Callback已经被调用了”。例如,如果我发送10条消息,并将我的线程数设置为5,前5条消息将顺利进行,但随后我开始在消息6或7周围看到重复的回调错误(见下文)。你知道我怎样才能避免这个错误吗

我的测试文件(其中定义了异步队列):

我在控制台中的结果:

Sending message 1: Hello_1  
Sending message 2: Hello_2
Sending message 3: Hello_3
Sending message 4: Hello_4
Sending message 5: Hello_5
Response 1: myResponse
Sending message 6: Hello_6
Response 2: myResponse
Sending message 7: Hello_7
Response 3: myResponse
Sending message 8: Hello_8
Response 4: myResponse
Sending message 9: Hello_9
Response 5: myResponse
Sending message 10: Hello_10
Response 6: myResponse
Response 7: myResponse
Error: Callback was already called.
    at myFile.js:12:34
如果我取消对myService.callback=null行的注释,则最后一批的第一次发送会导致myService.callback过早为null。例如,如果我发送10个线程数为5的请求,请求1到5将非常有效。但是,一旦我发送了从1到10的请求,请求#10就会过早地使myService.callback无效。答复示例:

Sending message 1: Hello_1  
Sending message 2: Hello_2
Sending message 3: Hello_3
Sending message 4: Hello_4
Sending message 5: Hello_5
Response 1: myResponse
Sending message 6: Hello_6
Response 2: myResponse
Sending message 7: Hello_7
Response 3: myResponse
Sending message 8: Hello_8
Response 4: myResponse
Sending message 9: Hello_9
Response 5: myResponse
Sending message 10: Hello_10
Response 6: myResponse
the callback is NULL
the callback is NULL
the callback is NULL
the callback is NULL
我现在已经修好了

在我的测试文件中,我现在只是调用sendMyMessage;我不再期待sendMyMessage的回调。在drain中,我等待响应的总数,然后循环遍历这些响应

var myQueue = async.queue(function(options, callback){   
    var counter = options.counter;

    myService.sendMyMessage(options.text, counter);
    callback();
}, NUMBER_OF_THREADS);

myQueue.drain = function(){ 
    var myInterval = setInterval(function() {

    if (myService.responseCounter == myNumberOfMessages) {

      clearInterval(myInterval);
      for (var i = 0; i < myNumberOfMessages; i++) {
        assert.equal(myExpectedResponse,myService.responses[i],"error");
      }
      done();
    }
  }, 5000);

};  

for (var j = 1; j <= myNumberOfMessages; j++) {
  var options = {
    counter: j,
    text: "Hello"
  };

  myQueue.push(options);
}
Sending message 1: Hello_1  
Sending message 2: Hello_2
Sending message 3: Hello_3
Sending message 4: Hello_4
Sending message 5: Hello_5
Response 1: myResponse
Sending message 6: Hello_6
Response 2: myResponse
Sending message 7: Hello_7
Response 3: myResponse
Sending message 8: Hello_8
Response 4: myResponse
Sending message 9: Hello_9
Response 5: myResponse
Sending message 10: Hello_10
Response 6: myResponse
the callback is NULL
the callback is NULL
the callback is NULL
the callback is NULL
var myQueue = async.queue(function(options, callback){   
    var counter = options.counter;

    myService.sendMyMessage(options.text, counter);
    callback();
}, NUMBER_OF_THREADS);

myQueue.drain = function(){ 
    var myInterval = setInterval(function() {

    if (myService.responseCounter == myNumberOfMessages) {

      clearInterval(myInterval);
      for (var i = 0; i < myNumberOfMessages; i++) {
        assert.equal(myExpectedResponse,myService.responses[i],"error");
      }
      done();
    }
  }, 5000);

};  

for (var j = 1; j <= myNumberOfMessages; j++) {
  var options = {
    counter: j,
    text: "Hello"
  };

  myQueue.push(options);
}
myService.sendLineMessage = function(message, counter) {

myModel.post(content, sign, config, request, log, run)
    .then(function(res) {
        myService.callbacks[counter] = function(result) {
            var myText = result.content.text;
            myService.responses.push(resultText);
        };
    });
};

myService.otherService = function(done) {
    app = express();
    app.use(express.bodyParser());

    app.post('/myRoute/events', function(req, res, next) {

    var response = {
        "myId": "1234567890",
        "myVersion": 1
    };

    res.set('Content-Type', 'application/json;charset=UTF-8');
    res.send(JSON.stringify(response));

    myService.responseCounter++;

    if (myService.callbacks[myService.responseCounter])
    {
        myService.callbacks[myService.responseCounter](req.body);
        myService.callbacks[myService.responseCounter] = null;
    }
    else
    {
        console.log('the callback is NULL');
    }
});