Promise.resolve 返回未定义 [英] Promise.resolve returns Undefined

查看:168
本文介绍了Promise.resolve 返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是多重承诺 - 在哪里解决的延续?我对不同的功能使用相同的代码.

This is a continuation of Multiple Promises - Where to resolve? where I use the same code for different function.

然而,这次 Promise.resolve 返回的是 undefined.

结果感谢多人指出错误.代码中有多个错误,我意识到我犯了.

Results Thanks to multiple people pointing out the mistakes. There are multiple errors in the code which I realised I committed.

1) 使用 &&在非布尔运算中.

1) Using && in a non Boolean operation.

应该使用

(console.log(results) , Promise.resolve(results)

代替

console.log(results) && Promise.resolve(results)

2) 使用不需要的 Promise.resolve - 仅从 Async 函数返回结果将产生与使用 Promise.resolve 相同的结果.

2) Using unneeded Promise.resolve - just return the results from the Async function will yield the same result as using Promise.resolve.

我的最终代码.

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 }))

}

原始问题我正在使用 nodejs promisify,所以我将以下内容声明为 Redis 的承诺

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))

},

然后我调用函数如下

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' ]//这是我控制台日志messagesId

[ '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 messagesId

房间 4 中的消息 => undefined//这是我控制台日志结果的时间

Messages in Room 4 => undefined //This is when i console log results

推荐答案

console.log() 返回 undefined,这是错误的.&& 是一个短路运算符,只有在第一个表达式为真时才计算第二个表达式.所以它永远不会执行 Promise.resolve(messagesId).

console.log() returns undefined, which is falsey. && is a short-circuiting operator, and only evaluates the second expression when the first expression is truthy. So it's never executing Promise.resolve(messagesId).

使用逗号运算符代替 &&.它计算它的两个表达式并返回第二个.

Instead of &&, use the comma operator. It evaluates both of its expressions and returns the second one.

    .then(messagesId => (console.log(messagesId), Promise.resolve(messagesId)))

这篇关于Promise.resolve 返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆