fastjs-team / core

Fastjs is a useful, lightweight JavaScript library for any types of project.
https://fastjs.dev
MIT License
358 stars 40 forks source link

Error Handling Utility: Simplify Asynchronous Code in JavaScript #136

Open zeroqs opened 1 day ago

zeroqs commented 1 day ago

What is the feature?

This feature provides a utility function for JavaScript that simplifies asynchronous code handling by returning results and errors. Developers can easily manage success and failure cases without writing verbose try...catch blocks.

Example:

const [error, users] = await catchError(fetchUsers());

Why is this feature needed?

This feature introduces a clean, declarative way to manage errors, making the code more concise and easier to maintain.

Any additional comments?

No response

Confirmations

xiaodong2008 commented 10 hours ago

Thanks, that's a good idea really.

Btw, I think the code should be

const [error, users] = await catchError(fetchUsers);
// or
const [error, users] = await catchError(() => fetchUsers());