gpt4 book ai didi

javascript - 如何让两个promise同步执行?

转载 作者:行者123 更新时间:2023-12-01 00:39:18 24 4
gpt4 key购买 nike

我正在尝试从 Redis 获取一些数据,用它做一些事情,然后将其存储回来。所以我有两次对redis的调用:

_.each(guids, async (guid) => {
const targetRecordSl = await this.redisConn.ft_searchAsync(this.TargetIndex, [`@guid:{${guid}}`, 'RETURN', 1, 'sl']);
console.log('Trying to add sl:' + slId + ' to: '+guid+ ' existing value:'+ targetRecordSl[2][1]);
let sl = '';
if(targetRecordSl[2][1] != '_null' && !targetRecordSl[2][1].includes(slId)) {
sl = targetRecordSl[2][1] + ', ' + slId;
} else {
sl = slId;
}
const response = await this.redisConn.ft_addAsync(this.TargetIndex, [`${this.TargetIndex}:${guid}`, 1, 'REPLACE', 'PARTIAL', 'FIELDS', 'sl', sl]);
console.log(response);

第一个查询执行循环的所有迭代,而不等待第二个查询完成:

Trying to add sl:11 to: P6d43d914bea8b91ece2a0a3c081b9b85 existing value:10
Trying to add sl:10 to: P6d43d914bea8b91ece2a0a3c081b9b85 existing value:10
Trying to add sl:9 to: P24e58b30a0658b8f0e5f9ce1ca0acc1f existing value:9
Trying to add sl:8 to: P7345d54686bfd491747eefd4e05d0362 existing value:8
OK
OK
OK
OK

这不是我想要的。我希望一个 promise 等待另一个完成:

Trying to add sl:11 to: P6d43d914bea8b91ece2a0a3c081b9b85 existing value:10
OK
Trying to add sl:10 to: P6d43d914bea8b91ece2a0a3c081b9b85 existing value:10
OK
Trying to add sl:9 to: P24e58b30a0658b8f0e5f9ce1ca0acc1f existing value:9
OK
Trying to add sl:8 to: P7345d54686bfd491747eefd4e05d0362 existing value:8
OK

请多多指教。

最佳答案

也许你可以尝试用普通的 for 循环来做到这一点:

for (const guid of guids) {
const targetRecordSl = await this.redisConn.ft_searchAsync(this.TargetIndex, [`@guid:{${guid}}`, 'RETURN', 1, 'sl']);

console.log('Trying to add sl:' + slId + ' to: ' + guid + ' existing value:' + targetRecordSl[2][1]);

let sl = '';
if (targetRecordSl[2][1] != '_null' && !targetRecordSl[2][1].includes(slId)) {
sl = targetRecordSl[2][1] + ', ' + slId;
} else {
sl = slId;
}

const response = await this.redisConn.ft_addAsync(this.TargetIndex, [`${this.TargetIndex}:${guid}`, 1, 'REPLACE', 'PARTIAL', 'FIELDS', 'sl', sl]);

console.log(response);
}

如果这是顶级代码,您可以尝试:

async function processGuids(guids) {
for (const guid of guids) {
const targetRecordSl = await this.redisConn.ft_searchAsync(this.TargetIndex, [`@guid:{${guid}}`, 'RETURN', 1, 'sl']);

console.log('Trying to add sl:' + slId + ' to: ' + guid + ' existing value:' + targetRecordSl[2][1]);

let sl = '';
if (targetRecordSl[2][1] != '_null' && !targetRecordSl[2][1].includes(slId)) {
sl = targetRecordSl[2][1] + ', ' + slId;
} else {
sl = slId;
}

const response = await this.redisConn.ft_addAsync(this.TargetIndex, [`${this.TargetIndex}:${guid}`, 1, 'REPLACE', 'PARTIAL', 'FIELDS', 'sl', sl]);

console.log(response);
}
}

processGuids(guids)
.then(() => {
console.log('JOB DONE!!')
// Here goes your following code (if it has to be executed after processing all guids)
});

关于javascript - 如何让两个promise同步执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57833671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com