的我想要實現一個簡單的提綱:快速路由和異步代碼 - 如何發送異步代碼的結果?
- 一個AJAX調用到特定的URL進行的服務器上
- 表達路線(以「app.js」),然後運行節點「Send.js」中的.js代碼
- 「Send.js」的輸出用作「res.send」方法對AJAX請求的響應。
我的問題是,「Send.js」利用異步方法等的「迴應」變量我試圖返回「res.send(響應)」沒有定義當響應發送,因爲「Send.js」中的「sendOn」函數在異步代碼執行之前完成,當然。
我知道使用回調很可能是這裏的解決方案,所以「res.send(response)」不會被調用,直到定義了響應,我不知道如何實現,這裏兩個獨立的文件:
app.js(簡化):
var send = require("./Files/Other/Send.js");
app.post('/Lamp-On', function (req, res)
{
var response = send.sendOn();
res.send(response);
});
Send.js(簡化):
client.open和client.sendEvent是接受一個回調既是異步方法最後一個參數,並且是外部SDK的一部分。
var sendMessage = function()
{
var data = "on";
var message = new Message(data);
message.properties.add('Turn On');
console.log('Sending message to turn the lamp: ' + message.getData());
client.sendEvent(message, printResultFor('Message'));
};
var connectCallback = function() {
if (err) {
console.error('Could not connect: ' + err.message);
}
else {
console.log('Client connected');
client.on('message', function (msg) {
console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
client.complete(msg, printResultFor('completed'));
// reject and abandon follow the same pattern.
// /!\ reject and abandon are not available with MQTT
});
client.on('error', function (err) {
console.error(err.message);
});
client.on('disconnect', function() {
clearInterval(sendInterval);
client.removeAllListeners();
client.connect(connectCallback);
});
// Now call the sendMessage function.
sendMessage();
}
};
var sendOn = function() {
client.open(connectCallback);
return
};
// Helper function to print results in the console
function printResultFor(op) {
return function printResult(err, res) {
if (err !== null) {
console.log(op + ' error: ' + err.toString());
console.log(err);
}
if (res) {
console.log(op + ' status: ' + res.constructor.name);
};
};
}
exports.sendOn = sendOn;
我只是不怎麼處理這麼多的異步回調!
最後注意:我既不問也不指望任何人爲我做這件事,但我希望你能指點我正確的方向,事先要感謝。
我的建議是使用[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)來處理異步調用,您將能夠這樣做'send.sendOn()。then(function(response){res.send(response);});' –
我建議使用'async'模塊來避免這個問題'https://github.com/caolan/async' – abdulbarik