This is a continuation of Multiple Promises - Where to resolve? where I use the same code for different function.
However, this time the Promise.resolve is returning undefined.
Results Thanks to multiple people pointing out the mistakes. There are multiple errors in the code which I realised I committed.
1) Using && in a non Boolean operation.
should use
(console.log(results) , Promise.resolve(results)
instead of
console.log(results) && Promise.resolve(results)
2) Using unneeded Promise.resolve - just return the results from the Async function will yield the same result as using Promise.resolve.
My final codes.
getMessages: function (roomId) {
return keysAsync('room:'+roomId)
.then(room =>
room === '' ? Promise.reject('Invalid room Id')
: smembersAsync('room:messages:'+roomId))
.then(messagesId => { return messagesId })
.catch(err => { return err }))
}
Original Question I'm using nodejs promisify so I have the followings declared as promise for Redis
const { promisify } = require('util');
const getAsync = promisify(client.get).bind(client);
const hmsetAsync = promisify(client.hmset).bind(client);
const hsetAsync = promisify(client.hset).bind(client);
const incrAsync = promisify(client.incr).bind(client);
const smembersAsync = promisify(client.smembers).bind(client);
const keysAsync = promisify(client.keys).bind(client);
const sismemberAsync = promisify(client.sismember).bind(client);
getMessages: function (roomId) {
return keysAsync('room:'+roomId)
.then(room =>
room === '' ? Promise.reject('Invalid room Id')
: smembersAsync('room:messages:'+roomId))
.then(messagesId => console.log(messagesId) && Promise.resolve(messagesId))
.catch(err => Promise.reject(err))
},
And then i call the function as follows
tools.getMessages('4').then((results) => {
console.log('Messages in Room 4 => ', results);
}).catch(err => console.log(err))
In my console, I can see the following results
[ '191', '192', '193', '194', '195', '196', '197',
'198', '199', '200', '201', '202', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218' ] //this is when i console log messagesIdMessages in Room 4 => undefined //This is when i console log results