Convert promise chains to async/await syntax for better readability, error handling, and proper cleanup operation placement. Async functions automatically return promises, so avoid redundant Promise.resolve() calls or manual promise wrapping.
Convert promise chains to async/await syntax for better readability, error handling, and proper cleanup operation placement. Async functions automatically return promises, so avoid redundant Promise.resolve() calls or manual promise wrapping.
Key benefits:
Example transformation:
// Before: Complex promise chain with misplaced finally
destroy(deletedBy, opts) {
return this.setDeletedState(deletedBy).then(() => {
// operation
}).finally(() => {
// cleanup only applies to inner promise
});
}
// After: Clean async/await with proper cleanup scope
async destroy(deletedBy, opts) {
this.set("isDeleting", true);
try {
await this.setDeletedState(deletedBy);
// operation
} finally {
// cleanup applies to entire operation
this.set("isDeleting", false);
}
}
Avoid returning Promise.resolve() from async functions as they already return promises automatically.
Enter the URL of a public GitHub repository