function observerFn(observer:Observer<number>) {
const seq = [1,2,3,4];
let timeoutId:any;
function doSequence(arr:number[], idx:number) {
timeoutId = setTimeout(() => {
observer.next(arr[idx]);
if(idx==arr.length-1){
observer.complete();
}else{
doSequence(arr, ++idx);
}
}, 1000);
}
doSequence(seq,0);
return ()=>{ clearInterval(timeoutId) };
}
const sequence = new Observable(observerFn);
sequence.subscribe({
next(value) {
console.log(value);
},
complete() {
console.log('completed');
},
});