async function getRedisKeyAsync(key){
var value = await client.get(key);
console.log('got value ' value ' for key ' key ' in redis');
return value;
}
async function getRedisKeyPromise(key){
client.get(key).then(
(value) => {
console.log('got value ' value ' for key ' key ' in redis');
return value;
}
);
}
In both cases, value for key is fetched from redis correctly. But a caller of these functions get the value back only in the first case.
var value1 = await myredis.getRedisKeyAsync(req.params.key); // works
var value2 = await myredis.getRedisKeyPromise(req.params.key); // value2 is undefined
How do I rewrite getRedisKeyPromise so that it behaves the same as getRedisKeyAsync ?
CodePudding user response:
Try by adding return
to second function
async function getRedisKeyPromise(key) {
return client.get(key).then(
(value) => {
console.log('got value ' value ' for key ' key ' in redis');
return value;
}
);
}
CodePudding user response:
You can just simplify it:
function getRedisKeyPromise(key){
return client.get(key)
}
It does the equivalent task like async or just .then()