0

I have the following function:

async function updateData() {
  return firebase.database().ref(varurl3).child('UserCount/count').once('value').then(function(snapshot) {
    var username = snapshot.val();
    var username2 = username + 1;
    return firebase.database().ref(varurl3).child('Users').child(username2).child('User').once('value').then(function(snapshot) {
      var username3 = snapshot.val();
      if (username3 != null) {
        await Sleep(3000);
      }
    });
  });
}
timeupdater = setInterval(updateData, 100);

I have marked my function as "async" but the following error message still appears: "Uncaught SyntaxError: await is only valid in async functions and async generators". Can anyone tell me how I can solve this problem? I would appreciate a helpful answer.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    Only your outermost `updateData` function is marked `async`. You need to make the innermost `function(snapshot) { ... }` async in order to use `await` within it – Phil Oct 06 '20 at 22:46
  • `then` is another function – Keith Oct 06 '20 at 22:47
  • using `.then` inside an `async` function is usually bad code - I wouldn't say never use it (I've seen one example where it's the easiest solution to a problem) but, in this case, it's not – Jaromanda X Oct 06 '20 at 23:06
  • Also, it may not be obvious, but `updateData` **will** be called every 100ms, and the sleep does nothing anyway – Jaromanda X Oct 06 '20 at 23:07

0 Answers0