Promise
let promise = new Promise(resolve => {
let timer = setTimeout(() => {
resolve('promise resolve');
}, 2000);
});
promise.then(value => {
console.log('value = ', value)
});
Rxjs
let stream = new Observable(observer => {
let timer = setTimeout(() => {
observer.next('observer...');
}, 2000);
});
let handle = stream.subscribe(value => console.log(value));
另 Rxjs :
// 取消订阅
setTimeout(()=>{
handle.unsubscribe();
}, 1000);
// 连续触发
let stream2 = new Observable(observer => {
let count = 0;
let timer = setInterval(() => {
observer.next(count);
count++;
}, 1000);
});
stream2.subscribe(value => console.log('stream2 = ', value));