如何等待蓝鸟承诺在多个地点定居?

2024-03-17

我遇到的情况是,一堆函数需要等待承诺解决,因为它是 init 函数;

self.init=new Promise(function(resolve){
   //do stuff, take awhile
   resolve();
});

但是,当它正在初始化时,异步性质意味着依赖于它的其他函数正在被调用。我希望这些函数等待 init 完成,然后继续。

我尝试在每个函数中执行此操作

function doSomethingUseful(){
    self.init.reflect().then(function () {
       //do functions purpose
    });
}
function doSomethingUseless(){
    self.init.reflect().then(function () {
       //do functions purpose
    });
}

但它只能随机工作,可能只有在 init 已经解决的情况下才有效,如果没有解决,它就会挂在这里,奇怪地挂起整个应用程序,尽管它是异步的。

我正在尝试替换以前的解决方案,该解决方案涉及间隔并在每个函数调用中检查布尔 isInit。

有没有蓝鸟函数可以做到这一点?或者另一种继续等待并检查承诺以查看其是否得到解决的方法?

该应用程序在很多地方都有这种结构。通常围绕 sqlite 读/写。用于打开数据库的 init,但在打开时,页面正在加载,并且它已经在尝试读/写表,因此通过使用 setInterval 并重复检查 init 是否完成来强制这些读/写等待。

这是一个使用谷歌分析的例子。

function Analytics() {
    var self = this;
    self.ready = ko.observable(false).subscribeTo('application:ready'); //attached to page ready event in jquerymobile and cordova
    self.trackerInit = new Promise(function (resolve, reject) {
        ko.computed(function () {
            if (self.ready()) {
                window.ga.startTrackerWithId('id', 1000, resolve, reject);
            }
        });
    });
}

Analytics.prototype.trackSpeed = function (cat, interval, variable, label) {
    var self = this;
    console.log("speed tracker", cat, interval, variable, label); //this logs
    return self.trackerInit.then(function () {
        console.log("speed tracker confirm init"); //this never logs, all execution stops including other async code        
        return new Promise(function (resolve, reject) {
            window.ga.trackTiming(cat, interval, variable, label, resolve, reject);
        });
    }).catch(function (e) {
        if (e.message === "send timeout") {
            return true; //who cares about timeouts anyways
        } else {
            throw e;//rethrow it
        }
    });
};

函数在页面更改事件中调用,没有返回,纯异步。调用它会导致所有执行停止。

准备好的ko就这样完成了

self.ready = ko.observable(false).publishOn('application:ready');

var deviceReady = new Promise(function (resolve) {
    $(document).on('deviceready', resolve);
});
var pageReady = new Promise(function (resolve) {
    $(document).on('pagecreate', resolve);
});

Promise.all([deviceReady, pageReady]).then(function () {
   //a couple of page of code and...
   self.ready(true);
});

在检查其结果时,像这样更改 init 会产生相同的挂起结果

self.trackerInit = new Promise(function (resolve, reject) {
    console.log("initting");
    checker = setInterval(function () {
        if (window.ga) {
            console.log("ready init");
            window.ga.startTrackerWithId('id', 100, function(){
                clearInterval(checker);
                console.log("init complete");
                resolve();
            }, reject);
        }
    }, 1000);
});

它们只是承诺。只需使用then把他们拴起来

function doSomethingUseful() {
  // wait for init to finish, then do our stuff
  // return the new chained promise in case someone wants to wait on us
  return self.init.then(function () {
    // do stuff
  });
}

function doSomethingUseless() {
  // wait for init to finish, then do our stuff
  // return the new chained promise in case someone wants to wait on us
  return self.init.then(function () {
    // do stuff
  });
}


// do both of those things and then do something else!
Promise.all([doSomethingUseful(), doSomethingUseless()]).then(function () {
  console.log("init is done.  And we've done something useful and useless.")
}

Edit:

根据您的附加代码,问题是,如果应用程序在构建 Analytics 组件之前已“准备好”,那么您将永远不会收到“application:ready”(因为它在您订阅之前出现),因此您的“准备好”可观察值将保持错误。根据邮箱文档,您需要通过true作为第二个参数subscribeTo这样即使它发生在过去,您也会获得就绪值:

ko.observable(false).subscribeTo("application:ready", true)

然而,仅仅为了兑现承诺而构建所有这些可观察和计算的结果就太过分了。怎么样:

self.trackerInit = new Promise(function (resolve, reject) {
    const s = ko.postbox.subscribe("application:ready", function (value) {
       if (value) {
           s.dispose(); // stop listening (prevent memory leak
           window.ga.startTrackerWithId('id', 1000, resolve, reject);
       }
    }, true);
});

你甚至可以把它变成一个承诺助手:

function whenReady(eventName) {
    return new Promise((resolve, reject) => {
        const s = ko.postbox.subscribe(eventName, value => {
            if (ready) {
                s.dispose();
                resolve(value);
            }
        }, true);
    });
}

function startGaTracker(id, timeout) {
    return new Promise((resolve, reject) => window.ga.startTrackerWithId(id, timeout, resolve, reject);
}

然后你可以写:

self.trackerInit = whenReady("application:ready")
    .then(() => startGaTracker("id", 100));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何等待蓝鸟承诺在多个地点定居? 的相关文章