nodejs/expressjs中的多个函数如何运行?在 php 中,它很简单,一个接一个地调用一个函数,但是节点中的这个回调业务令人困惑,我不断收到未定义变量等错误。这是我正在做的事情的基本想法。
var express = require('express');
var request = require('request');
var app = express();
app.get('/user/:id', function(req, res) {
var id = req.params.id;
getInformation(id, function(info) {
res.send(info);
});
});
app.listen(3000);
getInformation(id, callback) {
var qty = makeExternalApiCall();
var color = secondFunction(id);
callback({quantity: qty, color: color});
}
makeExternalApiCall() {
request({uri: 'https://provider.com/api/stuff/'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
return body.qty;
}
}
}
secondFunction(id) {
//look up color by id
var color = "blue";
return color;
}