簡體   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');
  }
});

我可以將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)

我想說這是一個很好的做法,盡管將它們命名為resolvereject ,因為 web 上的大多數示例都使用它,所以程序員最終會期待它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM