Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 1.09 KB

prefer-await-to-then.md

File metadata and controls

61 lines (47 loc) · 1.09 KB

Prefer await to then()/catch()/finally() for reading Promise values (promise/prefer-await-to-then)

Valid

async function example() {
  let val = await myPromise()
  val = doSomethingSync(val)
  return doSomethingElseAsync(val)
}

async function exampleTwo() {
  try {
    let val = await myPromise()
    val = doSomethingSync(val)
    return await doSomethingElseAsync(val)
  } catch (err) {
    errors(err)
  }
}

Invalid

function example() {
  return myPromise.then(doSomethingSync).then(doSomethingElseAsync)
}

function exampleTwo() {
  return myPromise
    .then(doSomethingSync)
    .then(doSomethingElseAsync)
    .catch(errors)
}

function exampleThree() {
  return myPromise.catch(errors)
}

function exampleFour() {
  return myPromise.finally(cleanup)
}

Options

strict

Normally, this rule allows then or catch following an await (or yield) expression. Setting this option to true will err on such cases:

This will fail with the strict option:

async function hi() {
  await thing().then()
}