RxJS bindNodeCallback

2020-10-13 18:33 更新

將Node.js樣式的回調(diào)API轉(zhuǎn)換為返回Observable的函數(shù)。

bindNodeCallback<T>(callbackFunc: Function, resultSelector: Function | SchedulerLike, scheduler?: SchedulerLike): (...args: any[]) => Observable<T>

參量

callbackFunc 類型:Function。
resultSelector 類型:Function | SchedulerLike。
調(diào)度器 可選的。默認(rèn)值為undefined。用于調(diào)度回調(diào)的調(diào)度程序。

returns

(...args: any[]) => Observable<T>:返回Observable的函數(shù),該函數(shù)傳遞與Node.js回調(diào)傳遞的值相同的值。

描述

就像 bindCallback,但是回調(diào)應(yīng)該是 type callback(error, result)

bindNodeCallback 不是運(yùn)算符,因為其輸入和輸出都不是可觀察的。輸入是 func 帶有某些參數(shù)的函數(shù),但最后一個參數(shù)必須 func 是完成后調(diào)用的回調(diào)函數(shù)?;卣{(diào)函數(shù)應(yīng)遵循 Node.js 約定,其中回調(diào)的第一個參數(shù)是錯誤對象,表示調(diào)用是否成功。如果將該對象傳遞給回調(diào),則表示出了點問題。

輸出的 bindNodeCallbackfunc 與最后一個參數(shù)(回調(diào))相同的,與相同的參數(shù)的函數(shù)。當(dāng)使用參數(shù)調(diào)用輸出函數(shù)時,它將返回一個 Observable。如果 func 使用錯誤參數(shù)調(diào)用其回調(diào),則 Observable 也將使用該值發(fā)生錯誤。如果未傳遞錯誤參數(shù),則 Observable 將發(fā)出第二個參數(shù)。如果有更多參數(shù)(第三個,依此類推), Observable 將發(fā)出一個數(shù)組,其中包含除第一個錯誤參數(shù)以外的所有參數(shù)。

請注意, func 不會同時調(diào)用輸出函數(shù),而是在訂閱結(jié)果 Observable 時調(diào)用。默認(rèn)情況下,對的調(diào)用 func 將在訂閱后同步發(fā)生,但是可以通過將其 scheduler 作為可選的第三個參數(shù)適當(dāng)提供來進(jìn)行更改。SchedulerLike 還可以控制 Observable 何時發(fā)出回調(diào)中的值。要了解更多信息,請查閱的文檔 bindCallback,其 SchedulerLike 工作原理完全相同。

與中一樣 bindCallback,this 輸入函數(shù)的上下文(屬性)在被調(diào)用時將被設(shè)置為返回函數(shù)的上下文。

Observable 發(fā)出值后,它將立即完成。這意味著即使 func調(diào)用再次回調(diào),來自第二次和連續(xù)調(diào)用的值也將永遠(yuǎn)不會出現(xiàn)在流中。如果您需要處理多次調(diào)用回調(diào)的函數(shù),請簽出 fromEventfromEventPattern 代替。

請注意,它 bindNodeCallback 也可以在非 Node.js 環(huán)境中使用?!?Node.js樣式”回調(diào)只是一個約定,因此,如果您是為瀏覽器或任何其他環(huán)境編寫的,并且您使用的 API 實現(xiàn)了該回調(diào)樣式,那么 bindNodeCallback也可以在該 API 函數(shù)上安全地使用它。

請記住,傳遞給回調(diào)的 Error 對象不必是 JavaScript 內(nèi)置 Error 對象的實例。實際上,它甚至不需要對象。當(dāng)回調(diào)函數(shù)的錯誤參數(shù)的值為真時,將其解釋為“存在”。例如,它可以是非零數(shù)字,非空字符串或 boolean true。在所有這些情況下,所得的 Observable 均會因該值而出錯。這意味著通常的常規(guī)樣式回調(diào)在 bindNodeCallback 使用時會經(jīng)常失敗 。如果您的 Observable 錯誤發(fā)生的次數(shù)比您預(yù)期的要多得多,請檢查是否確實以 Node.js 樣式調(diào)用了回調(diào),如果不是,則切換為 bindCallback。

請注意,即使 error 參數(shù)在技術(shù)上存在于回調(diào)中,但其值是錯誤的,它仍然不會出現(xiàn)在 Observable 發(fā)出的數(shù)組中。

例子

從文件系統(tǒng)讀取文件并以 Observable 形式獲取數(shù)據(jù)

import * as fs from 'fs';
const readFileAsObservable = bindNodeCallback(fs.readFile);
const result = readFileAsObservable('./roadNames.txt', 'utf8');
result.subscribe(x => console.log(x), e => console.error(e));

在具有多個參數(shù)的函數(shù)調(diào)用回調(diào)上使用

someFunction((err, a, b) => {
  console.log(err); // null
  console.log(a); // 5
  console.log(b); // "some string"
});
const boundSomeFunction = bindNodeCallback(someFunction);
boundSomeFunction()
.subscribe(value => {
  console.log(value); // [5, "some string"]
});

用于常規(guī)樣式的函數(shù)調(diào)用回調(diào)

someFunction(a => {
  console.log(a); // 5
});
const boundSomeFunction = bindNodeCallback(someFunction);
boundSomeFunction()
.subscribe(
  value => {}             // never gets called
  err => console.log(err) // 5
);

超載

bindNodeCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>

參量: 類型:
callbackFunc Function。resultSelector類型:Function。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any>

bindNodeCallback(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any調(diào)度器可選的。默認(rèn)值為undefined 類型:SchedulerLike。

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。 調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns() => Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>

參量: 類型:
callbackFunc (callback: (err: any, res1: R1, res2: R2) => any) => any。 調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns() => Observable<[R1, R2]>

bindNodeCallback(callbackFunc: (callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>

參量: 類型:
callbackFunc (callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike。

returns() => Observable<R1>

bindNodeCallback(callbackFunc: (callback: (err: any) => any) => any, scheduler?: SchedulerLike): () => Observable<void>

參量: 類型:
callbackFunc (callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike。

returns() => Observable<void>

bindNodeCallback(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。 調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1) =>Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>

參量: 類型:
callbackFunc (arg1: A1, callback: (err: any, res1: R1, res2: R2) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1) => Observable<[R1, R2]>

bindNodeCallback(callbackFunc: (arg1: A1, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>

參量: 類型:
callbackFunc (arg1: A1, callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1) => Observable<R1>

bindNodeCallback(callbackFunc: (arg1: A1, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>

參量: 類型:
callbackFunc (arg1: A1, callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1) => Observable<void> bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2) => Observable<[R1, R2]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2) => Observable<R1>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2) => Observable<void>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]> bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<R1>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3) => Observable<R1>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<void>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3) => Observable<void>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2, R3]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2, R3]>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]> bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>

bindNodeCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>

參量: 類型:
callbackFunc (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any) => any) => any。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>

bindNodeCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>

參量: 類型:
callbackFunc Function。調(diào)度器可選的。默認(rèn)值為undefined。類型:SchedulerLike

returns(...args: any[]) => Observable<any[]>

也可以看看

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號