What you're trying to do is something every JavaScript programmer (myself included) attempts and fails at somewhere in their career. You simply can't make it work the way you are hoping.
You can set a global variable from inside a promise or other asynchonous callback, and it looks like your code should do that.
However, this does not solve the other problem you mentioned: these callbacks are run asynchronously.
Somewhere, you have some code that wants to look at your valid
variable and take some action based on it. But how does this code know when your promise callback has completed, so valid
is valid (pun intended)? It won't.
What you need to do is put that code in a function, call it from your callback, and pass the valid
flag as an argument. That way the function will be run at the right time, after the callback has determined whether there are any invalid users.
And now you don't need the global or the second promise callback. It might look something like this:
"use strict";
firebase.database().ref(path).once('value')
.then((snap) => {
const data = snap.val();
if (data) { // You shouldn't need the '!== null'
for (const user in data) { // There was a 'const' missing here
if (data[user].Email === Email) {
allUsersAreValid(false);
return;
}
}
}
allUsersAreValid(true);
});
function allUsersAreValid( valid ) {
// Do something here
}
Also I added a "use strict";
at the top of your code to put it in strict mode, and recommend you always do that too. The for (user in data)
inadvertently creates a global variable user
because there is no const
or let
or var
there. Strict mode would prevent this error.