可观察的订阅的输出是意外的

时间:2018-10-03 05:10:42

标签: rxjs6

我有以下可观察的代码:

import {bindNodeCallback} from 'rxjs';
export type ErrorFirstCb = (err: any, val?: any) => void;
export type CbFn = (m: number, cb: ErrorFirstCb) => void;

const register = function (cb: CbFn) {

  let m = 0;

  setInterval(() => {

    m++;

    cb(m, (err, v) => {
      console.log('we got:', err, v);
    });

  }, 500);

  return cb;

};


const obs = bindNodeCallback(register((v, cb) => {

  setTimeout(() => {

    cb(null, v + 'sooo');

  }, 200);

}));


const subscription = obs(5).subscribe(v => {
  console.log('on next:', v);
}, e => {
  console.log('on error:', e);
});

您可以直接使用ts-node可执行文件运行它。我得到以下输出:

on next: 5sooo
we got: null 1sooo
we got: null 2sooo
we got: null 3sooo
we got: null 4sooo

我原本希望这样:

 on next: 5sooo
 on next: 1sooo
 on next: 2sooo
 on next: 3sooo
 on next: 4sooo

有人知道为什么不输出第二组吗?

0 个答案:

没有答案