在Javascript中的类样式对象的实例之后运行回调

时间:2017-03-27 16:08:56

标签: javascript fingerprintjs2

我正在接受其他人的代码而不确定我是否会以正确的方式做某事。

有一个Tracker'类'(基本上只是一个函数对象,包括'初始化器')。

班级将拨打第三方图书馆的电话。

返回结果后应该发生回调。

这是我正在尝试做的事情:

var tracker = Tracking.initialize({
        prod: 'OurProduct',
        requiredLayoutKeys: ["scr"],
        requiredInteractiveKeys: ["name", "action"],
        maxQueueSize: 5,
        timeToSend: 5000,
    });

// this code should happen in the callback once tracker is initialized:
InitSessionInfo(
    {sid: tracker.__sid, product: tracker.prod},
    function (returnObj) {
        setSessionIP(tracker.getIP(),function(returnObj){
            OurApp.auth(function (user) {
                OurApp.initialize(user);
            });
        });
    });

以下是Tracking对象在单独文件中的样子:

var Tracking = {
  __required_layout_keys: null,
  __required_interactive_keys: null,
  __maxQueueSize: null,
  __timeToSend: null,
  __initialized: false,

  initialize: function(config){
    Tracking.__initialized = true;
    Tracking.__required_interactive_keys = config.requiredInteractiveKeys;
    Tracking.__required_layout_keys = config.requiredLayoutKeys;
    Tracking.__maxQueueSize = config.maxQueueSize;
    Tracking.__timeToSend = config.timeToSend;
    Tracking.__bid = '';
    Tracking.__sid = '';
    Tracking.__ua = '';

    // get user data from FingerPrintJs2. 
    // The callback needs to happen after these results are returned: 
    new Fingerprint2().get(function(result, components){
        var ua = components.find(function(item){
            return item.key === "user_agent";
        });
        Tracking.__bid = result;
        Tracking.__sid = result + "." + Date.now();
        Tracking.__ua = ua.value;
    });
  },
 // there are other functions here that return ip, etc.
}

不幸的是,“跟踪器”总是以未定义的形式返回。

更新

在这里粘贴调试器时:

var tracker = Tracking.initialize({
    prod: 'OurProduct',
    requiredLayoutKeys: ["scr"],
    requiredInteractiveKeys: ["name", "action"],
    maxQueueSize: 5,
    timeToSend: 5000,
});

debugger;

跟踪器的值是“未定义”,并且跟踪的值尚未设置其“sid”值。

跟踪更新:

var Tracking = {
 ...
 initialize: function(){
 ...
 return new Fingerprint2().get(function(result, components){
        var ua = components.find(function(item){
            return item.key === "user_agent";
        });
        Tracking.__bid = result;
        Tracking.__sid = result + "." + Date.now();
        Tracking.__ua = ua.value;
        return Tracking;
    });


 }

1 个答案:

答案 0 :(得分:1)

因此,fingerprintjs2(在一系列内部函数调用的内部)返回setTimeout,这本质上是异步的,但不幸的是,您不能默认链接。通过查看setTimeout中的代码,您可以看到它在超时结束时调用您传递给get()函数的函数,因此您可以利用promises来确保它事情发生在应有的时候。

您需要做的是用new Fingerprint2().get()号召回一个承诺,然后在" done"内解决该承诺。传递给get函数的函数(在setTimeout中调用的函数)。

然后,您可以从initialize对象中的Tracking函数返回promise,以便您可以在成功初始化/更新数据后链接它并执行操作。

ES6 Promises & Chaining Docs

Working DEMO

使用Promise对象的ES6版本

var Tracking = {
  __required_layout_keys: null,
  __required_interactive_keys: null,
  __maxQueueSize: null,
  __timeToSend: null,
  __initialized: false,
  initialize: function(config){
    Tracking.__initialized = true;
    Tracking.__required_interactive_keys = config.requiredInteractiveKeys;
    Tracking.__required_layout_keys = config.requiredLayoutKeys;
    Tracking.__maxQueueSize = config.maxQueueSize;
    Tracking.__timeToSend = config.timeToSend;
    Tracking.__bid = '';
    Tracking.__sid = '';
    Tracking.__ua = '';
    // get user data from FingerPrintJs2. 
    // The callback needs to happen after these results are returned: 
    // return the promise so we can chain
    return new Promise(function (resolve, reject) {
        new Fingerprint2().get(function(result, components){
          var ua = components.find(function(item){
            return item.key === "user_agent";
          });
          Tracking.__bid = result;
          Tracking.__sid = result + "." + Date.now();
          Tracking.__ua = ua.value;
          resolve(Tracking); // resolve promise with updated Tracking obj
        });
    });
  },
  // there are other functions here that return ip, etc.
};

var tracker = null;
Tracking.initialize({
  prod: 'OurProduct',
  requiredLayoutKeys: ["scr"],
  requiredInteractiveKeys: ["name", "action"],
  maxQueueSize: 5,
  timeToSend: 5000,
}).then(function (updatedTracker) {
    tracker = updatedTracker;
    console.log(tracker);
});

使用$ .Deferred()

的ES5兼容版本

jQuery Deferred Promise Docs

这里的一小部分语法发生了变化,但没什么大不了的 - 而且这个想法完全相同。

var Tracking = {
  __required_layout_keys: null,
  __required_interactive_keys: null,
  __maxQueueSize: null,
  __timeToSend: null,
  __initialized: false,
  initialize: function(config){
    Tracking.__initialized = true;
    Tracking.__required_interactive_keys = config.requiredInteractiveKeys;
    Tracking.__required_layout_keys = config.requiredLayoutKeys;
    Tracking.__maxQueueSize = config.maxQueueSize;
    Tracking.__timeToSend = config.timeToSend;
    Tracking.__bid = '';
    Tracking.__sid = '';
    Tracking.__ua = '';
    // get user data from FingerPrintJs2. 
    // The callback needs to happen after these results are returned: 
    var deferred = $.Deferred(); // create deferred object
    new Fingerprint2().get(function(result, components){
          var ua = components.find(function(item){
            return item.key === "user_agent";
          });
          Tracking.__bid = result;
          Tracking.__sid = result + "." + Date.now();
          Tracking.__ua = ua.value;
          deferred.resolve(Tracking); // resolve deferred object with updated Tracking obj
        });
    return deferred.promise(); // return deferred promise that we can chain off of
  },
  // there are other functions here that return ip, etc.
};

var tracker = null;
// jQuery deferreds work with $.when(promise).then() syntax
$.when(Tracking.initialize({
  prod: 'OurProduct',
  requiredLayoutKeys: ["scr"],
  requiredInteractiveKeys: ["name", "action"],
  maxQueueSize: 5,
  timeToSend: 5000,
})).then(function (updatedTracker) {
    tracker = updatedTracker;
    console.log(tracker);
});