简体   繁体   English

Java脚本承诺-如果语句返回最佳实践

[英]Java Script Promises - If Statement Return Best Practices

I'm writing a node.js function that returns a different promise depending on a condition, the cod: 我正在编写一个node.js函数,该函数根据条件cod返回不同的promise:

if(condition){
    return promise.then(() => {
        return Promise.resolve(value)
    })
}else{
    return anotherPromise
}

Now the problem is that if the condition is true, I need to something after the promise is fulfilled, but in the other case I just return the promise, so the eslint tells me that it's a bad practice to nest promises. 现在的问题是,如果条件为真,则在实现诺言之后我需要执行一些操作,但在另一种情况下,我只是返回诺言,所以eslint告诉我嵌套诺言是一种不好的做法。 So this code won't work for me: 所以这段代码对我不起作用:

(() => {
    if(condition){
        return promise
    }
    }else{
        return anotherPromise
    }
}).then(() => {
    return Promise.resolve(value)
})

Because using this code the then callback will be executed in the two cases. 因为使用此代码,所以在两种情况下都将执行then回调。

What is the best practice to handle this case? 处理这种情况的最佳做法是什么?

If you use classic (ES6 / ES2015+) Promise syntax you have to chain promises (nothing bad with it!). 如果您使用经典(ES6 / ES2015 +)Promise语法,则必须链接promise(这没什么不好的!)。

But you have also option to split the code into functions to gain readability and avoid nesting issues : 但是,您也可以选择将代码拆分为多个函数,以提高可读性并避免嵌套问题

const firstCase = () => ... // returning a promise
const secondCase = () => ... // returning a promise

if (condition) {
  return firstCase()
} else {
  return secondCase()
}

But with ES7/ES2016+ you can use async/await syntax: 但是使用ES7 / ES2016 +,您可以使用async / await语法:

// in a "async" function
async function main() {
  if (condition) {
    await promise // if you need the promise result, you can assign it
    return value // the result of an async function is always a Promise.
  } else {
    return anotherPromise
  }
}

or mix both solutions. 或混合两种解决方案。

A simple suggestion, (this should work) pass the condition in the resolve's argument and check it in the then block. 一个简单的建议,(这应该起作用)在resolve的参数中传递条件, 然后then块中对其进行检查。 The pseudo-code below will clarify it better: 下面的伪代码将更好地阐明它:

(() => {
    if(condition){
        return new Promise((resolve,reject)=>{
            //Some tasks
            resolve(condition)

            //Some reject condition
            reject()
        })
    }
    else {
        return new Promise((resolve,reject)=>{
            //Some tasks
            resolve(condition)

            //Some reject condition
            reject()
        })
    }
}).then((condition) => {
     if(condition) {
         //Do something
     }
     else {
        //Do Nothing
     }
})

eslint tells me that it's a bad practice to nest promises. eslint告诉我,嵌套诺言是一种不好的做法。

Just tell it to shut *** up disable the linter on this statement. 只要告诉它关闭***,就可以在此语句禁用lint。 Promises have the ability to be nested precisely so that you can nest them when you need it, and this is one of those cases. 承诺具有精确嵌套的能力,因此您可以在需要时嵌套它们,这就是其中一种情况。 Your code is fine. 您的代码很好。

Seems like you are over complicating it. 似乎您已经使它复杂化了。 The then method already returns a promise so you don't need to put a Promise.resolve inside it. then方法已经返回了一个Promise,因此您无需在其中放入Promise.resolve。

You can for simplicity do 您可以为简单起见

return condition
  ? promise.then(() => value)
  : anotherPromise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM