簡體   English   中英

返回空的 Observable 時不調用訂閱回調

[英]Subscribe callback not called when empty Observable returned

我希望在返回空的 Observable 時調用subscribe回調,類似於Promise.resolve([])

import { EMPTY } from 'rxjs';

function funcToTest(): Observable<any[]> {
  return EMPTY;
};

test('returns empty array', (done) => {
  funcToTest().subscribe(() => {
    done();
  });
});

相反,它返回此錯誤:

Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

Observables 的行為與 promises 不同。 RxJS EMPTY Observable 不會調用“成功”回調,只會調用“完成”回調。 done函數應該在“完成”而不是“成功”中調用:

funcToTest().subscribe({
  success()  { /* Called when 'x' is returned, e.g. after the subscriber calls 'next' */ },
  error(err) { /* Called on an error. */ },
  complete() {
    /* Called after the subscriber calls 'complete'. No more values can be returned */
    done();
  }
});

請參閱文檔中的示例: https : //rxjs-dev.firebaseapp.com/guide/observable

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM