Promise中的代码执行和使用return语句 [英] Code Execution in Promise and using return statements

查看:42
本文介绍了Promise中的代码执行和使用return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,在使用Promise时,我需要返回 resolve reject 方法

Here, while using promise should i need to return the resolve and reject methods

代码执行平稳,但是如果有多个条件语句,则将拒绝 resolve 自动结束,否则我们必须使用return语句

the code executes smoothly but if there are multiple conditions statements then will reject and resolve auto-ends or we have to use return statements

 const getJobs = (filters, fieldASTs) => new Promise((resolve, reject) => {
        const AST = fieldASTs.fieldNodes[0].selectionSet.selections[0]
            .selectionSet.selections[0].selectionSet.selections;
        const FIELDS = _.map(AST, n => n.name.value);

        if (_.includes(FIELDS, 'employer')) {
            Job.find(filters, (err, d) => {
                if (err) return reject(err);

             // should i need to return or just use reject

                if (err === null && d === null) return reject(null);
             // return resolve(d) or only resolve()
                return resolve(d);
            });
        } else {
            Job.find(filters, (err, d) => {
              // here also
                if (err) return reject(err);
                // here too
                return resolve(d);
            });
        }
    });

推荐答案

是否使用 return 语句完全取决于函数中的期望控制流.它实际上与诺言无关.如果您不希望或不需要函数中的其他代码来执行并且还没有完全隔离在条件中,请使用 return 退出该函数.无论您是否使用诺言,都存在相同的问题.

Whether or not to use a return statement is entirely up the desired flow of control in your function. It actually has nothing to do with promises. If you don't want or need any more code in your function to execute and you aren't already completely isolated in a conditional, then use a return to exit the function. Same issue whether you are or aren't using promises.

请记住,所有 resolve() reject()所做的所有操作都是更改Promise的状态(假设其处于待处理状态),然后更改任何> .then() .catch()处理程序安排在当前运行的Javascript将控制权返回给系统之后,以供将来执行.它们只是函数调用,就像其他函数调用一样.

Remember, all resolve() or reject() do is change the state of the promise (assuming it is in the pending state) and then, any .then() or .catch() handlers are scheduled for future execution after the current running Javascript returns control back to the system. They are just functions calls like other functions calls.

在调用 resolve() reject()之后,您不必使用return语句.

You do not have to use return statements after calling resolve() or reject().

因此, return 语句是否合适完全取决于您的代码.如果您不想在该块中执行更多代码,请 return .如果您不想浪费时间潜在地在块中的其他位置调用 resolve() reject()(实际上对诺言没有任何作用),请使用 return .如果您的代码已经在条件块中,并且您不想执行的其他任何操作都没有,那么就不需要 return .

So, whether or not a return statement is appropriate depends entirely upon your code. If you don't want to execute more code in that block, then return. If you don't want to waste time potentially calling resolve() or reject() elsewhere in the block (which will not actually do anything to the promise), then use a return. If your code is already within a conditional block and nothing else will execute that you don't want to execute, then there is no need for a return.

例如,在您的代码的这一部分:

For example, in this part of your code:

if (_.includes(FIELDS, 'employer')) {
    Job.find(filters, (err, d) => {
        if (err) return reject(err);

        if (err === null && d === null) return reject(null);
        return resolve(d);
    });
 }

使用 return 是合适的,因为不需要在当前函数中执行更多代码.如果您在此处省略了 return ,则您的代码仍然可以正常工作(在这种情况下),因为您要运行的额外代码实际上不会执行任何操作,因为调用 reject() resolve()后,您已经拒绝或解决了承诺不会改变任何事情.但是,我认为让不需要运行的代码运行是一种浪费和混乱的做法.因此,在这种情况下,我总是使用 return 或有条件的.

it is appropriate to use the return because there is no need to execute any more code in the current function. If you omitted the return there, your code would still function fine (in this particular case) because the extra code you would run would not actually do anything since calling reject() or resolve() after you've already rejected or resolved the promise does not change anything. But, I consider it a wasteful and a bit confusing practice to let code run that doesn't need to run. So, I would always use either a return or a conditional in this case.

我个人可能会这样写代码:

Personally, I'd probably write this code like this:

if (_.includes(FIELDS, 'employer')) {
    Job.find(filters, (err, d) => {
        if (err) return reject(err);
        if (d === null) return reject(new Error("unexpected null result from Job.find()"));
        return resolve(d);
    });
}

注意:我删除了对 if(err === null)的检查,因为那应该是成功的案例.

Note: I removed the check for if (err === null) because that should be the success case.

或者,我本可以在较低的级别更普遍地承诺 Job.find(),这样我的逻辑流程就可以实现了.

Or, I would have promisified Job.find() more generically at a lower level so my logic flow would all be promises.

这篇关于Promise中的代码执行和使用return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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