/    Sign up×
Community /Pin to ProfileBookmark

Firebase RTDB – How to yield forEach loop until certain thing happens?

I’m using `snapshot.forEach(function(item) {})` to get each thing inside of a reference. The site I’m making is actually a live quiz game (like Kahoot, Quizizz, etc.) so I’m using the forEach function to get each question of a quiz. The thing is, I want to yield/pause the script at the end of the function, and wait for the player/user to answer the question, then stop yielding so it goes to the next question. The other day I figured out the `yield` operator. And it did as I thought: it paused the script at the first question. But then I tried putting `continue` right after it, for testing, and it still stayed at the first question. I also tried putting `yield` in a variable and doing `yield.next()`, because I saw that somewhere, but it did not work either. How can I sort of “continue”/unpause the script after a yield?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@BostauthorDec 01.2021 — Bumping because my question has not been answered yet.
Copy linkTweet thisAlerts:
@SempervivumDec 01.2021 — Check if you find an answer and a solution here:

https://stackoverflow.com/questions/29313604/can-i-yield-from-an-inner-function
Copy linkTweet thisAlerts:
@BostauthorDec 03.2021 — @Sempervivum#1640055 This did not help me. Because I don't think there's any other option besides forEach loop for a DataSnapshot from Firebase.
Copy linkTweet thisAlerts:
@SempervivumDec 03.2021 — You might try to transfer the snapshot to a javascript array as described here:

https://stackoverflow.com/questions/41427859/get-array-of-items-from-firebase-snapshot

Then it should be possible to use a for-of loop on that array.
Copy linkTweet thisAlerts:
@BostauthorDec 07.2021 — @Sempervivum#1640113 This is what I thought about doing at first, however my code uses the item that's in snapshot.forEach.
Copy linkTweet thisAlerts:
@SempervivumDec 08.2021 — I wonder why it's not possible to modify your code that way that it uses an item from the resulting array?

You intended to use `next</C> to get the next item from the result. This will return an object like:<br/>
<C>
{done: true, value: item_from_database}`


i. e. you would have to modify your code anyway.

In order to find a solution it might be helpful if you posted more of your code.
Copy linkTweet thisAlerts:
@BostauthorDec 11.2021 — @Sempervivum#1640298

> @Sempervivum#1640298 I wonder why it's not possible to modify your code that way that it uses an item from the resulting array?

How can I do this? I think this is exactly what I want.
Copy linkTweet thisAlerts:
@SempervivumDec 11.2021 — I tested this code and it's working fine:
``<i>
</i> const snapshot = [1, 2, 4, 8]; // just for testing

let helperArr = [];
snapshot.forEach(function (item) {
helperArr.push(item);
});
function* gen(arr) {
for (let item of arr) {
// return item and wait for next request:
yield item;
}
}
const g = gen(helperArr);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);<i>
</i>
``

Check if you can adjust it to your situation.
×

Success!

Help @Bost spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...