Skip to content

Files

Latest commit

 

History

History
46 lines (35 loc) · 900 Bytes

prefer-await-to-then.md

File metadata and controls

46 lines (35 loc) · 900 Bytes

Pattern: Use of then() instead of await

Issue: -

Description

Prefer await to then() for reading Promise values.

Example of correct code:

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)
  }
}

Example of incorrect code:

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

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

Further Reading