將回調 API 轉換為返回 Observable 的函數(shù)。
bindCallback<T>(callbackFunc: Function
, resultSelector?:Function
| SchedulerLike
, scheduler?: SchedulerLike
): (...args: any[]) =>Observable
<T>
callbackFunc | 類型:Function 。 |
---|---|
resultSelector | 可選的。默認值為 undefined 。類型:Function | SchedulerLike 。 |
調度器 | 可選的。默認值為 undefined 。用于調度回調的調度程序。 |
(...args: any[]) => Observable<T>
:返回 Observable 的函數(shù),該函數(shù)提供與回調傳遞的值相同的值。
給它一個 f
類型 f(x, callback)
的函數(shù) g
,它將返回一個函數(shù),當調用 as 時 g(x)
將輸出一個 Observable。
bindCallback
不是運算符,因為其輸入和輸出都不是可觀察的。輸入是 func
帶有某些參數(shù)的函數(shù)。最后一個參數(shù)必須 func
是完成后調用的回調函數(shù)。
輸出的 bindCallback
是 func
與最后一個參數(shù)(回調)相同的,與相同的參數(shù)的函數(shù)。使用參數(shù)調用輸出函數(shù)時,它將返回一個 Observable。如果函數(shù) func
使用一個參數(shù)調用其回調,則 Observable 將發(fā)出該值。另一方面,如果使用多個值調用回調,則產生的 Observable 將發(fā)出一個以所述值作為參數(shù)的數(shù)組。
這是非常重要的是要記住,輸入功能 func
,當輸出功能是不叫,而是當由輸出函數(shù)返回的可觀測訂閱。這意味著,如果 func
發(fā)出 AJAX 請求,則每次有人訂閱生成的 Observable 時都會發(fā)出該請求,但不是之前。
最后一個可選參數(shù)-- scheduler
可用于控制 func
在有人訂閱 Observable 之后何時進行調用,以及何時傳遞傳遞給回調的結果。默認情況下,對 Observable 的預訂是 func
同步進行的,但是將其 async
用作最后一個參數(shù)會將調用推遲到 func
,就像將調用包裝成 will setTimeout
超時0
一樣。如果要使用異步調度程序并 subscribe
在輸出 Observable 上調用,則當前正在執(zhí)行的所有函數(shù)調用將在 func
調用之前結束。
默認情況下,傳遞給回調的結果在 func
調用回調后立即發(fā)出。特別是,如果回調是同步調用的,則結果Observable 的訂閱也將 next
同步調用該函數(shù)。如果您想推遲該呼叫,則可以 async
像以前一樣使用。這意味著通過使用 Scheduler.async
您可以確保 func
始終異步調用其回調,從而避免使 Zalgo 感到恐懼。
請注意,由輸出函數(shù)創(chuàng)建的 Observable 將始終發(fā)出單個值,然后立即完成。如果 func
多次調用回調,則后續(xù)調用中的值將不會出現(xiàn)在流中。如果您需要收聽多個呼叫,則可能要使用 fromEvent
或 fromEventPattern
代替。
如果 func
依賴于某些上下文(this
屬性)并且尚未綁定,則的上下文 func
將是輸出函數(shù)在調用時具有的上下文。特別是,如果 if func
作為某些 objec 的方法被調用,并且 if func
尚未綁定,則為了保留上下文,建議將輸出函數(shù)的上下文也設置為該對象。
如果輸入函數(shù)以“節(jié)點樣式”調用其回調(即,回調的第一個參數(shù)是表示調用是否失敗的可選錯誤參數(shù)),bindNodeCallback
則將提供方便的錯誤處理,并且可能是更好的選擇。 bindCallback
會將此類函數(shù)與其他任何函數(shù)一樣對待,并且錯誤參數(shù)(無論是否傳遞)將始終被解釋為常規(guī)回調參數(shù)。
import { bindCallback } from 'rxjs';
import * as jQuery from 'jquery';
// Suppose we have jQuery.getJSON('/my/url', callback)
const getJSONAsObservable = bindCallback(jQuery.getJSON);
const result = getJSONAsObservable('/my/url');
result.subscribe(x => console.log(x), e => console.error(e));
import { bindCallback } from 'rxjs';
const someFunction = (a, b, c) => {
console.log(a); // 5
console.log(b); // 'some string'
console.log(c); // {someProperty: 'someValue'}
};
const boundSomeFunction = bindCallback(someFunction);
boundSomeFunction().subscribe(values => {
console.log(values) // [5, 'some string', {someProperty: 'someValue'}]
});
import { bindCallback } from 'rxjs';
function iCallMyCallbackSynchronously(cb) {
cb();
}
const boundSyncFn = bindCallback(iCallMyCallbackSynchronously);
const boundAsyncFn = bindCallback(iCallMyCallbackSynchronously, null, Rx.Scheduler.async);
boundSyncFn().subscribe(() => console.log('I was sync!'));
boundAsyncFn().subscribe(() => console.log('I was async!'));
console.log('This happened...');
// Logs:
// I was sync!
// This happened...
// I was async!
content_copyopen_in_newimport { bindCallback } from 'rxjs'; const boundMethod = bindCallback(someObject.methodWithCallback); boundMethod.call(someObject) // make sure methodWithCallback has access to someObject .subscribe(subscriber);
| bindCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>
參量: | 類型: |
---|---|
callbackFunc | Function |
resultSelector | Function |
scheduler | Optional. Default is undefined 類型:SchedulerLike |
returns (...args: any[]) => Observable<any>
| bindCallback(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): () => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any |
scheduler | Optional. Default is undefined. 類型:SchedulerLike |
returns() => Observable<any[]>
bindCallback(callbackFunc: (callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>
參量: | 類型: |
---|---|
callbackFunc | (callback: (res1: R1, res2: R2, res3: R3) => any) => any |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns() => Observable<[R1, R2, R3]>
|
bindCallback(callbackFunc: (callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>
參量: | 類型: |
---|---|
callbackFunc | (callback: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike 。 |
returns() => Observable<[R1, R2]>
bindCallback(callbackFunc: (callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>
參量: | 類型: |
---|---|
callbackFunc | (callback: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns() => Observable<R1>
bindCallback(callbackFunc: (callback: () => any) => any, scheduler?: SchedulerLike): () => Observable<void>
參量: | 類型: |
---|---|
callbackFunc | (callback: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns() => Observable<void>
bindCallback(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1) => Observable<any[]>
bindCallback(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2, R3]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, callback: (res1: R1, res2: R2, res3: R3) => any) => any 。 |
調度器 | 可選的。 默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1) => Observable<[R1, R2, R3]>
bindCallback(callbackFunc: (arg1: A1, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, callback: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1) => Observable<[R1, R2]>
bindCallback(callbackFunc: (arg1: A1, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, callback: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1) => Observable<R1>
bindCallback(callbackFunc: (arg1: A1, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, callback: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike 。 |
returns(arg1: A1) => Observable<void>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2) => Observable<any[]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2, res3: R3) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, callback: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2) => Observable<[R1, R2]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, callback: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2) => Observable<R1>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, callback: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2) => Observable<void>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3) => Observable<any[]>
|
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (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: (res1: R1, res2: R2, res3: R3) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (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: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<R1>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, callback: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3) => Observable<R1>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<void>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, callback: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3) => Observable<void>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1:A1, arg2: A2, arg3: A3, arg4: A4) => Observable<any[]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (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: (res1: R1, res2: R2, res3: R3) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2, R3]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (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: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (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: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: () => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<any[]>
參量: | 類型: |
---|---|
callbackFunc | (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any 。 |
調度器 | 可選的。默認值為undefined 類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<any[]>
|
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (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: (res1: R1, res2: R2, res3: R3) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2, R3]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (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: (res1: R1, res2: R2) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (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: (res1: R1) => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>
bindCallback(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: () => 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: () => any) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>
|
bindCallback(callbackFunc: (...args: (A | ((result: R) => any))[]) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R>
參量: | 類型: |
---|---|
callbackFunc | (...args: (A ((result: R) => any))[]) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(...args: A[]) => Observable<R>
bindCallback(callbackFunc: (...args: (A | ((...results: R[]) => any))[]) => any, scheduler?: SchedulerLike): (...args: A[]) => Observable<R[]>
參量: | 類型: |
---|---|
callbackFunc | (...args: (A ((...results: R[]) => any))[]) => any 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike |
returns(...args: A[]) => Observable<R[]>
bindCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>
參量: | 類型: |
---|---|
callbackFunc | Function 。 |
調度器 | 可選的。默認值為undefined 。類型:SchedulerLike 。 |
returns(...args: any[]) => Observable<any>
更多建議: