i'm using firebase, so my function has an async nested function and looks like this:
  var invld = 0;
  ref.child(userId).orderByChild('name').equalTo(listName).on("value", 
  function(snapshot) {
    if (snapshot.exists()){
      invld = 1;
      alert(invld);
      return false;
    }
  });
  alert(invld);
  if (invld == 1) {
    // exit from the main function
    overwritePrompt();
    return false;
  }else{
    // Save on firebase
    database.ref().child('users').child(userId).push('list').set(data);
  }
it doesn't work because var invld is always 0, being updated inside the async function, how can i fix this situation?
