firebase - Is there any way to break Javascript function within an anonymous function? -


i want check if data i'm insert allready exists on firebase, , if want break add function:

fbdb.addcampain=function (campain){          campiansref.once('value',function(snapshot){         snapshot.foreach(function(childsnapshot){            if(campain.name==childsnapshot.val().name){                console.log("campain allredy exists on db");               return false; //i want break addcampain function here!            }          });         });     var newcampainref =  campiansref.push();    campain.id = newcampainref.key();    newcampainref.set(campain,function(error){        if(error){            console.log("an error occured campain did not add db, error:" ,+error);            return false;        }        else{            console.log("campain succssesfuly added db");            return true;        }    });  }; 

what happens if campaign exists on database still continues actual adding code. there must way "break" addcampain function within anonymous function inside it, or pass "return false" main scope.

if add few console.log statements, you'll able see how code flows:

console.log('1. starting call firebase'); campaignsref.once('value',function(snapshot){     console.log('3. value firebase');     snapshot.foreach(function(childsnapshot){         console.log('4. inside childsnapshot');         if (campaign.name==childsnapshot.val().name){             console.log("campaign exists on db");             return false;         }         console.log('5. done snapshot.foreach');     }); }); console.log('2. started call firebase'); 

the output like:

1. starting call firebase 2. started call firebase 3. value firebase 4. inside childsnapshot 4. inside childsnapshot 4. inside childsnapshot 5. done snapshot.foreach 

this not entirely expected. 2. @ end of code block, fires right after 1. @ start. because on starts asynchronous load of data firebase. , since takes time, browser continues code after block. once data downloaded firebase's servers, invoke callback , can want. then, original context has finished.

there no way in javascript wait asynchronous function finish. while might appreciate if such way existed, users frustrated fact browser locks while call firebase out.

instead have 2 options:

  1. pass callback function
  2. return promise

i'm going use option 1 below, because firebase javascript sdk does.

fbdb.addcampaign=function (campaign, callback){     campaignsref.once('value',function(snapshot){         var isexisting = snapshot.foreach(function(childsnapshot){             if(campaign.name==childsnapshot.val().name){                 return true; // cancels enumeration , returns true indicate campaign exists             }         });         callback(isexisting);     }); }; 

you'd invoke like:

fb.addcampaign(campaign, function(isexisting) {     console.log(isexisting ? 'the campaign existed' : 'the campaign added'); }; 

note loading campaigns server check if specific campaign name exists pretty wasteful. if want campaign names unique, might store campaigns name.

campaignsref.child(campaign.name).set(campaign); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -