Enforce using async/await syntax over Promises (github/no-then)
February 5, 2024 ยท View on GitHub
๐ผ 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