[英]Is resolve, reject keyword in javascript?
const promise = new Promise((resolve, reject) => {
if(allWentWell) {
resolve('All things went well!');
} else {
reject('Something went wrong');
}
});
我可以将resolve
更改为res
吗? 或者resolve在这里有什么特殊含义吗?
不,它们不是关键字,您可以随意命名它们,但 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. 话虽如此,您可以随意命名这些 arguments,但它们的顺序必须正确。
const myFunction = function (x, y) {
if (allWentWell) {
x('All things went well!')
} else {
y('Something went wrong')
}
}
new Promise(myFunction)
我想说这是一个很好的做法,尽管将它们命名为resolve
和reject
,因为 web 上的大多数示例都使用它,所以程序员最终会期待它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.