Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.03 KB

no-then.md

File metadata and controls

54 lines (41 loc) · 1.03 KB

Enforce using async/await syntax over Promises (github/no-then)

💼 This rule is enabled in the ✅ recommended config.

Rule Details

Yes, you should use promises, but prefer async/await syntax instead of Promise.then() callback chaining.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

👎 Examples of incorrect code for this rule:

function countData(url) {
  return downloadData(url).then(data => {
    return data.length
  })
}
function getProcessedData(url) {
  return downloadData(url).catch(e => {
    console.log('Error occurred!', e)
    return null;
  })
}

👍 Examples of correct code for this rule:

async function countProcessedData(url) {
  const data = await downloadData(url);
  return data.length
}
async function getProcessedData(url) {
  try {
    return await downloadData(url)
  } catch (e) {
    console.log('Error occurred!', e);
    return null;
  }
}

Version

4.3.2