简体   繁体   English

javascript 中的 resolve, reject 关键字是什么?

[英]Is resolve, reject keyword in javascript?

const promise = new Promise((resolve, reject) => {
  if(allWentWell) {
    resolve('All things went well!');
  } else {
    reject('Something went wrong');
  }
});

Can I change resolve to res ?我可以将resolve更改为res吗? Or does resolve have special meaning here?或者resolve在这里有什么特殊含义吗?

No they aren't keywords you can name them whatever you want, but IMO you should keep the names as descriptive as possible.不,它们不是关键字,您可以随意命名它们,但 IMO 您应该尽可能保持名称的描述性。

 const promise = new Promise((res, rej) => { if(true) { res('All things went well;'); } else { rej('Something went wrong'); } }). promise.then(val=> console.log(val))

No. The Promise constructor expects a function and will pass two arguments to it: the first one being a function that resolves the promise, and the second one being a function that rejects the promise. No. The Promise constructor expects a function and will pass two arguments to it: the first one being a function that resolves the promise, and the second one being a function that rejects the promise. With that being said, you can name these arguments anything you want, but they must be in the correct order.话虽如此,您可以随意命名这些 arguments,但它们的顺序必须正确。

const myFunction = function (x, y) {
  if (allWentWell) {
    x('All things went well!')
  } else {
    y('Something went wrong')
  }
}

new Promise(myFunction)

I'd say it's a good practice though to keep them named resolve and reject , as most examples on the web use it so programmers end up expecting for it.我想说这是一个很好的做法,尽管将它们命名为resolvereject ,因为 web 上的大多数示例都使用它,所以程序员最终会期待它。

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

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