Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using promises: Bad example VS good example #29743

Closed
attilalearning opened this issue Oct 20, 2023 · 3 comments · Fixed by #29746
Closed

Using promises: Bad example VS good example #29743

attilalearning opened this issue Oct 20, 2023 · 3 comments · Fixed by #29746
Assignees
Labels
Content:JS JavaScript docs effort: small This task is a small effort.

Comments

@attilalearning
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

What specific section or headline is this issue about?

"This may be worse if you have race conditions"

What information was incorrect, unhelpful, or incomplete?

When I looked at the JS code:

const listOfIngredients = [];

doSomething()
  .then((url) => {
    // I forgot to return this
    fetch(url)
      .then((res) => res.json())
      .then((data) => {
        listOfIngredients.push(data);
      });
  })
  .then(() => {
    console.log(listOfIngredients);
    // Always [], because the fetch request hasn't completed yet.
  });

I could not really understand where the error was? Where should have been a return? Again, a simple good example would have been useful, one that particularly explains or points out where the return should have been in this bad example?

Further, I noticed, the "good" example was beneath the bad one, however, I could not see the difference! I had to copy paste the code in two notepads one by the other to NOTICE (after a minute or so) the actual difference was very subtle when reading... there was a { ... } bracket in the bad one, that was missing in the good one, etc.

What did you expect to see?

Please see the above section "What information was incorrect, unhelpful, or incomplete?"

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@attilalearning attilalearning added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Oct 20, 2023
@github-actions github-actions bot added the Content:JS JavaScript docs label Oct 20, 2023
@collinsnji
Copy link
Contributor

Adding an indication of where the return statement should have been in the bad example would clarify this a bit more. I can work on this.

@caugner caugner changed the title Bad example VS good example Using promises: Bad example VS good example Oct 25, 2023
@caugner
Copy link
Contributor

caugner commented Oct 25, 2023

Related: #29742

@bsmth bsmth removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Nov 1, 2023
@zfox23
Copy link
Contributor

zfox23 commented Nov 13, 2023

Adding an indication of where the return statement should have been in the bad example would clarify this a bit more. I can work on this.

I appreciate your willingness to work on this issue, @collinsnji! I will assign this issue to you. If you would no longer like to work on this issue, or we don't hear from you in a few days, I'll open the issue back up to the community.

Edit: I see you've opened #29746 to resolve this issue. Thanks!!

@zfox23 zfox23 added the effort: small This task is a small effort. label Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs effort: small This task is a small effort.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants