跳至內容

unicorn/no-await-in-promise-methods 正確性

此規則預設為開啟。

此規則的作用

禁止在 Promise 方法參數中使用 await

為什麼這是不好的?

在傳遞給 Promise.all()Promise.allSettled()Promise.any()Promise.race() 的 promise 上使用 await 可能是一個錯誤。

範例

此規則的錯誤程式碼範例

javascript
async function foo() {
  Promise.all([await promise, anotherPromise]);
  Promise.allSettled([await promise, anotherPromise]);
  Promise.any([await promise, anotherPromise]);
  Promise.race([await promise, anotherPromise]);
}

此規則的正確程式碼範例

javascript
async function foo() {
  Promise.all([promise, anotherPromise]);
  Promise.allSettled([promise, anotherPromise]);
  Promise.any([promise, anotherPromise]);
  Promise.race([promise, anotherPromise]);
}

參考資料

在 MIT 許可下發布。