2

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

有一个跟踪器“类”(基本上只是一个函数对象,包括一个“初始化器”)。

该类将调用第三方库。

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

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

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;

tracker 的值为“未定义”,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;
    });


 }
4

1 回答 1

1

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

你需要做的是new Fingerprint2().get()用一个 promise 包装调用,然后在done你传递给函数的 " " 函数(在 setTimeout 中被调用的函数)中解决这个 promise get

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

ES6 Promises & Chaining 文档

工作演示

使用 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 延迟承诺文档

这里的语法有一点点变化,但没有什么大的变化——而且这个想法是完全一样的。

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);
});
于 2017-03-27T17:56:10.543 回答