我想要创建一个类,其任务是轮询数据源,将信息整理成一个“警报”对象数组,然后将这些警报的子集传递给任何其他需要它们的类。
因为轮询是异步进行的(我从web服务请求数据),所以我假设我实际上需要返回的是一个承诺,当它实现时,将给出Alert对象的正确子集。
但很明显,我不明白如何做到这一点,因为应该返回承诺的方法还会返回其他东西。
到目前为止,这是我的密码。如您所见,我正在尝试将诺言存储在实例属性中,然后检索它:
export class AlertCollection {
constructor() {
this.alerts = null;
}
// poll the data sources for alert data; store a promise that resolves
// to an array of alerts
poll() {
this.alerts = this.pollTeapot()
.then( (arr) => {this.pollDeliverance(arr);} );
}
// return a promise that fulfils to an array of the alerts you want
filteredAlerts(filter) {
return this.alerts; // not filtering for now
}
// return a promise that fulfills to the initial array of alerts
pollTeapot() {
let process = (json) => {
json2 = JSON.parse(json);
return json2.map( (a) => new Alert(a) );
};
message = new MessageHandler("teapot", "alerts")
return message.request().then( (json) => {process(json);} );
}
// Modify the alerts based on the response from Deliverance.
// (But for the time being let's not, and say we did.)
pollDeliverance(alerts) {
return alerts;
}
}message.request()从web服务中返回一个承诺。这是可行的。如果我在process中快照pollTeapot()函数,就会得到正确的数据。
但是,如果我快照了来自filteredAlerts()的返回值,我就不会得到它。我也不会得到null (这至少是有意义的,尽管这是错误的)。我得到了类似于{ _45: 0, _81: 0, _65: null, _54: null }的东西。
在这一点上,任何指示都将是非常感谢的。(顺便说一句,这是对原住民的反应,如果这有帮助的话。)
发布于 2017-07-06 12:21:59
这将是一个很难描述的例子--我有一个工作示例,但它很复杂,因为我不得不“模拟”所有异步部件,使用function类而不是class关键字--但是想法是一样的!
这个答案有两部分。
alerts存储为实例变量是没有意义的。它们是异步的,在异步调用完成后才会存在。poll的初始调用上一般情况下,你们会像这样互相承诺
functionWhichReturnsPromise()
.then(functionPointer)
.then(function(result){
// some functionality, which can return anything - including another promise
});所以你的代码看起来就像
var alertCollection = new AlertCollection()
alertCollection.poll().then(function(alerts){
//here alerts have been loaded, and deliverance checked also!
});该类的代码将遵循以下内容:
export class AlertCollection {
constructor() {
}
// poll the data sources for alert data; store a promise that resolves
// to an array of alerts
poll() {
return this.pollTeapot()
.then(filteredAlerts)
.then(pollDeliverance);
}
// return a promise that fulfils to an array of the alerts you want
filteredAlerts(alerts) {
return alerts; // not filtering for now
}
// return a promise that fulfills to the initial array of alerts
pollTeapot() {
let process = (json) => {
json2 = JSON.parse(json);
return json2.map( (a) => new Alert(a) );
};
message = new MessageHandler("teapot", "alerts")
return message.request().then(process);
}
// Modify the alerts based on the response from Deliverance.
// (But for the time being let's not, and say we did.)
pollDeliverance(alerts) {
return alerts;
}
}几个音符
filteredAlerts可以做任何你喜欢做的事情,只要它返回一个结果数组pollDeliverance也可以做您想做的任何事情--如果需要调用另一个异步方法,请记住返回一个解析为警报数组的承诺--也许是从异步调用的结果中更新的。我已经创建了一个JSFiddle,它演示了这一点--使用一个简单的getJSON调用来复制其中一些异步特性。正如我提到的,这是一个复杂的过程,但演示了这个过程:
发布于 2017-07-06 12:23:27
我不确定我是否完全理解了你的问题,但我会尝试给你一个通用的解决方案,一个接一个的链接承诺。
someAsyncFunction().then(dataFromAsync1 => {
return anotherAsyncFunction(dataFromAsync1).then(dataFromAsync2 => {
return doSomethingWithData(dataFromAsync1, dataFromAsync2);
});
});https://stackoverflow.com/questions/44947465
复制相似问题