I'm trying to fetch some data from redis, do something with it, then store it back. So I have two calls to 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);
the first query executes all iterations of the loop without waiting for the second query to finish:
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
This is not what I want. I want one promise to wait for the other to finish:
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
Advices please.