Fetch New Data Based on Conditions

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Each time we eat correctly guess the word, we want to grab a new word to restart our game. This sets up a scenario where we want the getWord broadcaster to make another fetch request when the input box has all of the letters contained in the current word. We can do this by using an operator that will send a value of done if conditions are met and another operator that will tell the broadcaster to repeat when it receives a value of done.

John Lindquist: [0:00] Now let's try to support reloading the word if we guess it correctly. Lunisolar should try to automatically load a new word.

[0:12] The idea here and the logic here is that our game logic should be done if and will take the guess and if the guess exists and is not an empty string and also the guess does not include an asterisk, then pass down a done and allow it to repeat.

[0:33] Import repeat and make sure doneIf is imported. I'll hit Save here. I'll make our guess pointlessly and you'll see two abortings coming from our get URL, which is down here on line 173. This should not be aborting twice and what that makes me think is that when we implemented doneIf and repeat, it makes me think we canceled in both of them.

[1:01] If I check out doneIf you'll see we canceled here, and we also canceled inside of repeat, right here. I think in doneIf we don't want to cancel. We just want to pass down a done to our listener, which is a very subtle difference, but can have a big impact.

[1:21] I'll delete that line and try it out again. We'll guess dissension and now, we get a single aborting, which is what we want, but this is not loading a new word. This makes me think where is the setup happening, forget word where the broadcaster is set up again?

[1:40] That's inside of our share where everything is captured, and the broadcaster is set up. That's right here, the broadcaster is set up and we'll see that if we console.log right here, setupBroadcaster, I'll hit Save, and you'll see setupBroadcaster. I'll guess the word debenture, you'll see aborting, but repeat is not making this setup again.

[2:05] Where we messed up is we captured this inside of here, whereas when we repeat the second time around, it's going to need to be inside of the broadcaster, so it gets set up again. If we put this here, and hit Save, you'll see setupBroadcaster.

[2:22] I'll guess the word and now it's still not working. That's because right here you see we checked to see if cancel was false, or didn't exist, but share was invoked, cancel was assigned, but cancel is never unassigned. We need to say cancel and assign that to null, so that the next time through, this cancel will be null, and it will allow this code to fire again.

[2:50] Let's see that. We'll guess the word. You'll see setupBroadcaster was called again, and it did load a new word this time.

egghead
egghead
~ 14 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today