將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)度程序。 |
(...args: any[]) => Observable<T>
:返回Observable的函數(shù),該函數(shù)傳遞與Node.js回調(diào)傳遞的值相同的值。
就像 bindCallback
,但是回調(diào)應(yīng)該是 type callback(error, result)
。
bindNodeCallback
不是運(yùn)算符,因?yàn)槠漭斎牒洼敵龆疾皇强捎^察的。輸入是 func
帶有某些參數(shù)的函數(shù),但最后一個(gè)參數(shù)必須 func
是完成后調(diào)用的回調(diào)函數(shù)。回調(diào)函數(shù)應(yīng)遵循 Node.js 約定,其中回調(diào)的第一個(gè)參數(shù)是錯(cuò)誤對(duì)象,表示調(diào)用是否成功。如果將該對(duì)象傳遞給回調(diào),則表示出了點(diǎn)問題。
輸出的 bindNodeCallback
是 func
與最后一個(gè)參數(shù)(回調(diào))相同的,與相同的參數(shù)的函數(shù)。當(dāng)使用參數(shù)調(diào)用輸出函數(shù)時(shí),它將返回一個(gè) Observable。如果 func
使用錯(cuò)誤參數(shù)調(diào)用其回調(diào),則 Observable 也將使用該值發(fā)生錯(cuò)誤。如果未傳遞錯(cuò)誤參數(shù),則 Observable 將發(fā)出第二個(gè)參數(shù)。如果有更多參數(shù)(第三個(gè),依此類推), Observable 將發(fā)出一個(gè)數(shù)組,其中包含除第一個(gè)錯(cuò)誤參數(shù)以外的所有參數(shù)。
請(qǐng)注意, func
不會(huì)同時(shí)調(diào)用輸出函數(shù),而是在訂閱結(jié)果 Observable 時(shí)調(diào)用。默認(rèn)情況下,對(duì)的調(diào)用 func
將在訂閱后同步發(fā)生,但是可以通過將其 scheduler
作為可選的第三個(gè)參數(shù)適當(dāng)提供來進(jìn)行更改。SchedulerLike
還可以控制 Observable 何時(shí)發(fā)出回調(diào)中的值。要了解更多信息,請(qǐng)查閱的文檔 bindCallback
,其 SchedulerLike
工作原理完全相同。
與中一樣 bindCallback
,this
輸入函數(shù)的上下文(屬性)在被調(diào)用時(shí)將被設(shè)置為返回函數(shù)的上下文。
Observable 發(fā)出值后,它將立即完成。這意味著即使 func
調(diào)用再次回調(diào),來自第二次和連續(xù)調(diào)用的值也將永遠(yuǎn)不會(huì)出現(xiàn)在流中。如果您需要處理多次調(diào)用回調(diào)的函數(shù),請(qǐng)簽出 fromEvent
或 fromEventPattern
代替。
請(qǐng)注意,它 bindNodeCallback
也可以在非 Node.js 環(huán)境中使用。“ Node.js樣式”回調(diào)只是一個(gè)約定,因此,如果您是為瀏覽器或任何其他環(huán)境編寫的,并且您使用的 API 實(shí)現(xiàn)了該回調(diào)樣式,那么 bindNodeCallback
也可以在該 API 函數(shù)上安全地使用它。
請(qǐng)記住,傳遞給回調(diào)的 Error 對(duì)象不必是 JavaScript 內(nèi)置 Error
對(duì)象的實(shí)例。實(shí)際上,它甚至不需要對(duì)象。當(dāng)回調(diào)函數(shù)的錯(cuò)誤參數(shù)的值為真時(shí),將其解釋為“存在”。例如,它可以是非零數(shù)字,非空字符串或 boolean true
。在所有這些情況下,所得的 Observable 均會(huì)因該值而出錯(cuò)。這意味著通常的常規(guī)樣式回調(diào)在 bindNodeCallback
使用時(shí)會(huì)經(jīng)常失敗 。如果您的 Observable 錯(cuò)誤發(fā)生的次數(shù)比您預(yù)期的要多得多,請(qǐng)檢查是否確實(shí)以 Node.js 樣式調(diào)用了回調(diào),如果不是,則切換為 bindCallback
。
請(qǐng)注意,即使 error 參數(shù)在技術(shù)上存在于回調(diào)中,但其值是錯(cuò)誤的,它仍然不會(huì)出現(xiàn)在 Observable 發(fā)出的數(shù)組中。
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));
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"]
});
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[]>
更多建議: