Observable

class Reminder {
    constructor(behavior){
        this.behavior = behavior;
    }
    remind(cb) {
        this.behavior(cb)
    }
}
let reminder = new Reminder((cb)=>{
        cb.next();
        cb.error();
        cb.complete();
});
reminder.remind({
    next: () => console.log('next'),
    error: () => console.log('error'),
    complete: () => console.log('complete')
});
//
class Observable{
    constructor(behavior){
        this.behavior = behavior;
    }
    subscribe(observer){
        this.behavior(observer);
    }
}

const obs$ = new Observable((cb)=>{
        cb.next(1);
        cb.error();
        cb.complete();
});
const observer = {
    next: v => console.log('next',v),
    error: () => console.log('error'),
    complete: () => console.log('complete')
}
obs$.subscribe(observer)

  

posted @ 2021-05-07 19:44  scqilin  阅读(62)  评论(0编辑  收藏  举报