naming-cheatsheet icon indicating copy to clipboard operation
naming-cheatsheet copied to clipboard

Change asynchronous get to fetch suggestion.

Open haIIux opened this issue 2 years ago • 3 comments

In the example displayed currently in the notes, it suggests that the asynchronous function would return instantaneously when it could return errors or throw. In my opinion, it would be better to recommend a namespace in which would reflect that accordingly.

The current example:

You can use get when performing asynchronous operations as well:

async function getUser(id) {
  const user = await fetch(`/api/user/${id}`)
  return user
}

The proposed change:

fetch

You can use fetch when performing asynchronous operations as well:

async function fetchUser(id) {
  const user = await fetch(`/api/user/${id}`)
  return user
}

The ideaology behind my suggestion is as follows. When you're playing fetch with a dog, the dog does not always immediately return. The dog may get distracted or while returning drop it before returning to the handler, thus erroring out and dropping the return.

haIIux avatar Dec 14 '22 17:12 haIIux

Why did you request my review since my previous comment is still open and unaddressed?

cjbarth avatar Nov 15 '23 01:11 cjbarth

Why did you request my review since my previous comment is still open and unaddressed?

Appears actually to be an error with GitHub mobile. I had requested a review on a private repository.

I'll address your requested changes asap! I apologize for the inconvenience.

haIIux avatar Nov 15 '23 13:11 haIIux

I thing fetch should be in another cases.

get for returning value

const getUser = () => {
  return fetch('/user').then(res => res.json());
};

const user = await getUser();

console.log(user);

fetch for fetching data to some variable

let user;

const fetchUser = async () => {
  const res = await fetch('/user').then(res => res.json());

  user = res;
};

await fetchUser();

console.log(user);

letstri avatar Feb 16 '24 10:02 letstri