每1分钟回复一次? [英] return promise every 1 minute?
本文介绍了每1分钟回复一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法每隔1分钟连续回复一次承诺?
i正在尝试这样的事情,但它在开头只返回一次承诺:
startWork(){
this.dataService.startPing(details).then((result)=> {
this.timeSlotsRefresh();
},(err)=> {
console.log(错误);
});
}
然后:
startPing(){
let startingTime = new Date();
返回新的Promise((resolve,reject)=> {
let source = Rx.Observable.timer(startingTime,60000).timeInterval()。pluck(' interval');
this.Subscription = source
.subscribe(data => {
this.http.post('http: // localhost:63203 / api / Ping',JSON.stringify(this.offlinePings [i]))
.map(res => res.json())
.subscribe(data => ; {
resolve(data);
},(err)=> {
reject(err);
});
});
});
}
它必须基本上通知这个函数每隔1分钟调用 this.timeSlotsRefresh();
来刷新数据,我该如何实现呢?
解决方案
@Injectableclass Ping {readonly observable = Rx.Observable.interval(60000); subscribe(... cbs){return this.observable.subscribe(... cbs); @ Componentclass Foo实现了OnInit,onDestroy {private subscription = null;构造函数(私有ping:Ping){} onPing(count){} onPingError(error){} onPingFinish(){} ngOnInit(){this.subscription = this.ping.subscribe((... d)=> this .onPing(... d),(... e)=> this.onPingError(... e),(... f)=> this.onPingFinish(... f)); } ngOnDestroy(){this.subscription.unsubscribe()}}
承诺
s只能工作一次,你可能需要类似于流媒体的东西, Observable
s可能适合更好。
var source = Rx .Observable .interval(2000 / * ms * /)。map(id => fetch(`https:\ / \ / jsonplaceholder.typicode.com \ / posts \ / $ {id}` ).then(res => res.json())); var subscription = source .subscribe(post => console.log('New Post',post)) ;
< script src =https:// cdnjs .cloudflare.com / AJAX /库/ rxjs / 5.4.0 / Rx.js>< /脚本>
is there any way to return a promise every 1 minute continuously? i was trying something like this but it returns promise only once at the beginning:
startWork() {
this.dataService.startPing(details).then((result) => {
this.timeSlotsRefresh();
}, (err) => {
console.log(err);
});
}
and then:
startPing() {
let startingTime = new Date();
return new Promise((resolve, reject) => {
let source = Rx.Observable.timer(startingTime, 60000).timeInterval().pluck('interval');
this.Subscription = source
.subscribe(data => {
this.http.post('http://localhost:63203/api/Ping', JSON.stringify(this.offlinePings[i]))
.map(res => res.json())
.subscribe(data => {
resolve(data);
}, (err) => {
reject(err);
});
});
});
}
it has to basically inform this function every 1 minute to call this.timeSlotsRefresh();
to refresh the data, how can i achieve that?
解决方案
@Injectable
class Ping {
readonly observable = Rx.Observable.interval(60000);
subscribe(...cbs) {
return this.observable.subscribe(...cbs);
}
}
@Component
class Foo implements OnInit, onDestroy {
private subscription = null;
constructor(private ping: Ping) {}
onPing(count) {}
onPingError(error) {}
onPingFinish() {}
ngOnInit() {
this.subscription = this.ping.subscribe(
(...d) => this.onPing(...d),
(...e) => this.onPingError(...e),
(...f) => this.onPingFinish(...f)
);
}
ngOnDestroy() {
this.subscription.unsubscribe()
}
}
Promise
s are meant to work only once, you may need for something similar to a streaming and Observable
s could suit better.
using rx
with the interval
operator:
var source = Rx
.Observable
.interval(2000 /* ms */)
.map(id => fetch(`https:\/\/jsonplaceholder.typicode.com\/posts\/${id}`).then(res => res.json()))
;
var subscription = source
.subscribe(post => console.log('New Post', post))
;
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.0/Rx.js"></script>
这篇关于每1分钟回复一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文