有什么办法可以破坏匿名函数中的Javascript函数? [英] Is there any way to break Javascript function within an anonymous function?

查看:34
本文介绍了有什么办法可以破坏匿名函数中的Javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查要插入所有准备就绪的数据在Firebase上是否存在,如果可以,我只想破坏add函数:

I just want to check if the data I'm about to insert allready exists on my Firebase, and if so I just want to break the 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 the DB");
              return false; //I want to break the addCampain function from here!
           }
         });
        });

   var newCampainRef =  CampiansRef.push();
   campain.id = newCampainRef.key();
   newCampainRef.set(campain,function(error){
       if(error){
           console.log("an error occured the campain did not add to the DB, error:" ,+error);
           return false;
       }
       else{
           console.log("campain succssesfuly added to the DB");
           return true;
       }
   });

};

当前发生的情况是,即使该活动存在于数据库中,它仍会继续执行实际的添加代码.必须有一种方法可以在其中的匿名函数内破坏" addCampain 函数,甚至可以将"return false"传递给主作用域.

What currently happens is that even if the campaign exists on the database it still continues to the actual adding code. There must be a way to "break" the addCampain function within an anonymous function inside it, or even pass the "return false" up to the main scope.

推荐答案

如果添加一些 console.log 语句,您将能够看到代码的流向:

If you add a few console.log statements, you'll be able to see how your code flows:

console.log('1. starting call to Firebase');
CampaignsRef.once('value',function(snapshot){
    console.log('3. for value from Firebase');
    snapshot.forEach(function(childSnapshot){
        console.log('4. inside childSnapshot');
        if (campaign.Name==childSnapshot.val().Name){
            console.log("campaign already exists on the DB");
            return false;
        }
        console.log('5. done with snapshot.forEach');
    });
});
console.log('2. we started the call to Firebase');

输出将如下所示:

1. starting call to Firebase
2. we started the call to Firebase
3. for value from Firebase
4. inside childSnapshot
4. inside childSnapshot
4. inside childSnapshot
5. done with snapshot.forEach

这可能不完全是您所期望的. 2.在代码块的末尾,但它在开始的 1.之后触发.这是因为 on 会从Firebase启动数据的异步加载.并且由于这需要时间,因此浏览器在该块之后继续执行代码.从Firebase的服务器下载数据后,它将调用回调,您可以执行所需的操作.但是到那时,原始上下文已经结束.

This is probably not entirely what you expected. 2. is at the end of the code block, but it fires right after 1. which is at the start. This is because on starts an asynchronous load of the data from Firebase. And since this takes time, the browser continues with the code after the block. Once the data is downloaded from Firebase's servers, it will invoke the callback and you can do what you want. But by then, the original context has finished.

JavaScript中没有办法等待异步函数完成.尽管您可能会喜欢这种方法,但您对用户的失望是,在您调用Firebase时,他们的浏览器锁定了.

There is no way in JavaScript to wait for an asynchronous function to finish. While you might appreciate if such a way existed, your users would be frustrated by the fact that their browser locks up while your call to Firebase is out.

相反,您有两个选择:

  1. 将回调传递给函数
  2. 兑现诺言

我将使用下面的选项1,因为这是Firebase JavaScript SDK已经执行的操作.

I'm going to use option 1 below, because it is what the Firebase JavaScript SDK already 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; // this cancels the enumeration and returns true to indicate the campaign already exists
            }
        });
        callback(isExisting);
    });
};

您可以这样调用

FB.addCampaign(campaign, function(isExisting) {
    console.log(isExisting ? 'The campaign already existed' : 'The campaign was added');
};

请注意,从服务器加载所有广告系列以检查特定的广告系列名称是否已经存在非常浪费.如果您希望广告系列名称唯一,则最好按名称存储广告系列.

Note that loading all campaigns from the server to check if a specific campaign name already exists is pretty wasteful. If you want campaign names to be unique, you might as well store the campaigns by name.

CampaignsRef.child(campaign.Name).set(campaign);

这篇关于有什么办法可以破坏匿名函数中的Javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆