DEV Community

Discussion on: JavaScript: Async/Await Wrapper

Collapse
 
vishalraj82 profile image
Vishal Raj

I would rather
export const asyncWrapper = async (fn, ...args) => {
Secondly, a general practice I have often seen is that error is always the first param, while the rest are the response from the function call.

return [error, response];

Collapse
 
dewaldels profile image
Dewald Els • Edited

I initially used a closure and thought of using the rest operator there, but I didn't like the syntax. Although, it does afford the ability to create functions to use later.

const fetchUsers = asyncWrapper(fetchUsersRequest);

// some other code
const [ users, error ] = await fetchUsers();
Enter fullscreen mode Exit fullscreen mode

I didn't see a need for this right now and decided to simplify it.

Collapse
 
dewaldels profile image
Dewald Els

Hey! Thanks for the feedback! I do prefer the flexibility of the rest params, but for this example I didn't have a need for it. 😊 It would be great to refine the function as the use cases start changing.