gpt4 book ai didi

javascript - 如何等待 Bluebird promise 在多个地点落户?

转载 作者:行者123 更新时间:2023-11-29 20:51:38 46 4
gpt4 key购买 nike

我遇到这样一种情况,一堆函数需要等待一个 promise 来解决,因为它是 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 已经解决时才工作,如果没有,它就卡在这里,奇怪地挂起整个应用程序,尽管它是异步的。

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

是否有 Bluebird 功能可以做到这一点?或者继续等待并检查 promise 以查看它是否已解决的另一种方式?

该应用在很多地方都有这种结构。通常围绕 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);
});

最佳答案

它们只是 promise 。只需使用 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.")
}

编辑:

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

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

但是,构建所有这些可观察值和计算值只是为了提供一个 promise 是矫枉过正的。怎么样:

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

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

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

关于javascript - 如何等待 Bluebird promise 在多个地点落户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51563091/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com