运行 Javascript 中 class 样式对象实例后的回调

run a callback after an instance of a class-style object in Javascript

我正在接受别人的代码,但不确定我是否打算以正确的方式做某事。

有一个 Tracker 'class'(基本上只是一个函数对象,包括一个 'initializer')。

class 将调用第三方库。

返回结果后应进行回调。

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

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);
            });
        });
    });

这是单独文件中的跟踪对象的样子:

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.
}

不幸的是 'tracker' 返回时总是未定义。

更新:

在此处粘贴调试器时:

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

debugger;

tracker 的值为 'undefined',Tracking 的值尚未设置 '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;
    });


 }

所以,fingerprintjs2 是(在一系列内部函数调用的深处)returning a setTimeout,它本质上是异步的,但不幸的是你不能链接默认。通过查看 setTimeout 中的代码,您可以看到它正在调用您在超时结束时传递给 get() 函数的函数,因此您可以利用承诺来确保事情发生在他们应该发生的时候。

您需要做的是用承诺包装 new Fingerprint2().get() 调用,然后在您设置的“done”函数(在 setTimeout 中调用的函数)中解决该承诺传入 get 函数。

然后,您可以 return 您的 Tracking 对象中 initialize 函数的承诺,以便您可以链接它并在数据成功后执行操作 initialized/updated。

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);
});