async / await /。然后在继续操作之前不等待函数结束。
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
谢谢您的帮助
答案 0 :(得分:1)
您不会将内部Promise与外部Promise链链接在一起。更改为
async function getInfosDatastore(req, res, next) {
const { IP, username, password } = req.body;
const url = 'https://simplivity@' + IP + '/api';
const token = await getInfos.getToken(url, username, password);
const [name, policy, cluster] = await Promise.all([
getInfos.getDatastores(token, url),
getInfos.getPolicies(token, url),
getInfos.getClusters(token, url)
]);
res.json({ name, policy, cluster });
}
使用await Promise.all
,在解释器进入下一条语句之前,将等待所有内部Promise(并提取其解析值)。
此外,请确保将catch
放在呼叫者getInfosDatastore
上,以防其中一个Promises拒绝(如果您尚未)。
答案 1 :(得分:0)
是正确的,因为函数体内的以下3个回调函数是在下一个事件循环(将来)中执行的
div.item[data-selected] {
border: 2px solid #333;
}
您需要做的是删除<div class="item" data-selected="">
ABC
</div>
Promise方法,但要使用# vocab size is 10, OH vector size is 3
embedding = nn.Embedding(10, 3)
# mini batch input 2 samples of 4 indices each
mb_input = torch.LongTensor([[1,2,5,6],[5,6,8,9]])
embedding(input)