我遇到这样的情况,因为它是init函数,因此一堆函数需要等待promise的履行;
self.init=new Promise(function(resolve){
//do stuff, take awhile
resolve();
});
但是,当它处于初始化状态时,异步性质意味着依赖于它被初始化的其他函数也将被调用。我希望这些功能等待初始化完成,然后继续。
我尝试在每个函数中这样做
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并反复检查初始化是否已完成,迫使这些读/写工作
这是使用Google Analytics(分析)的示例。
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会在检查结果时产生与hang相同的结果
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);
});
答案 0 :(得分:2)
它们只是承诺。只需使用SIC Value AREA
350 100 Education
460 500 Land
140 200 Forest
290 400 Education
506 450 Land
将它们链接起来
then
编辑:
根据您的其他代码,问题在于,如果在构建Analytics(分析)组件之前应用已“就绪”,那么您将永远不会收到“ application:ready”(因为它是在您订阅之前出现的),因此您的“就绪”可观察的将保持为假。根据邮箱文档,您需要将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.")
}
作为第二个参数传递给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);
}