【发布时间】:2019-06-24 09:49:35
【问题描述】:
async / await / .then 在继续之前不会等待函数结束。
async function getInfosDatastore(req, res, next) {
var IP = req.body.IP;
var username = req.body.username;
var password = req.body.password;
var token = '';
var cluster = "";
var policies = "";
var datastores = "";
url = 'https://simplivity@' + IP + '/api';
await getInfos.getToken(url, username, password)
.then(response => {
token = response;
getInfos.getDatastores(token, url)
.then(response => {
datastores = response;
});
getInfos.getPolicies(token, url)
.then(response => {
policies = response;
});
getInfos.getClusters(token, url)
.then(response => {
cluster = response;
});
});
res.json({ name: datastores, policy: policies, cluster: cluster });
}
输出是: - 令牌 - 测试 - res.json(但它是空的) - 每个函数中的console.log
应该是: - 令牌 - 每个函数中的console.log - 测试 - 具有正确值的 res.json
感谢您的帮助
【问题讨论】: