RxJS bindCallback

2020-10-13 18:32 更新

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

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

參量

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

returns

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

描述

給它一個(gè) f類型 f(x, callback)的函數(shù) g,它將返回一個(gè)函數(shù),當(dāng)調(diào)用 as 時(shí) g(x)將輸出一個(gè) Observable。

bindCallback 不是運(yùn)算符,因?yàn)槠漭斎牒洼敵龆疾皇强捎^察的。輸入是 func 帶有某些參數(shù)的函數(shù)。最后一個(gè)參數(shù)必須 func 是完成后調(diào)用的回調(diào)函數(shù)。

輸出的 bindCallbackfunc 與最后一個(gè)參數(shù)(回調(diào))相同的,與相同的參數(shù)的函數(shù)。使用參數(shù)調(diào)用輸出函數(shù)時(shí),它將返回一個(gè) Observable。如果函數(shù) func 使用一個(gè)參數(shù)調(diào)用其回調(diào),則 Observable 將發(fā)出該值。另一方面,如果使用多個(gè)值調(diào)用回調(diào),則產(chǎn)生的 Observable 將發(fā)出一個(gè)以所述值作為參數(shù)的數(shù)組。

這是非常重要的是要記住,輸入功能 func,當(dāng)輸出功能是不叫,而是當(dāng)由輸出函數(shù)返回的可觀測(cè)訂閱。這意味著,如果 func 發(fā)出 AJAX 請(qǐng)求,則每次有人訂閱生成的 Observable 時(shí)都會(huì)發(fā)出該請(qǐng)求,但不是之前。

最后一個(gè)可選參數(shù)-- scheduler可用于控制 func在有人訂閱 Observable 之后何時(shí)進(jìn)行調(diào)用,以及何時(shí)傳遞傳遞給回調(diào)的結(jié)果。默認(rèn)情況下,對(duì) Observable 的預(yù)訂是 func 同步進(jìn)行的,但是將其 async用作最后一個(gè)參數(shù)會(huì)將調(diào)用推遲到 func,就像將調(diào)用包裝成 will setTimeout超時(shí)0一樣。如果要使用異步調(diào)度程序并 subscribe在輸出 Observable 上調(diào)用,則當(dāng)前正在執(zhí)行的所有函數(shù)調(diào)用將在 func調(diào)用之前結(jié)束。

默認(rèn)情況下,傳遞給回調(diào)的結(jié)果在 func調(diào)用回調(diào)后立即發(fā)出。特別是,如果回調(diào)是同步調(diào)用的,則結(jié)果Observable 的訂閱也將 next 同步調(diào)用該函數(shù)。如果您想推遲該呼叫,則可以 async像以前一樣使用。這意味著通過使用 Scheduler.async 您可以確保 func 始終異步調(diào)用其回調(diào),從而避免使 Zalgo 感到恐懼。

請(qǐng)注意,由輸出函數(shù)創(chuàng)建的 Observable 將始終發(fā)出單個(gè)值,然后立即完成。如果 func 多次調(diào)用回調(diào),則后續(xù)調(diào)用中的值將不會(huì)出現(xiàn)在流中。如果您需要收聽多個(gè)呼叫,則可能要使用 fromEventfromEventPattern 代替。

如果 func 依賴于某些上下文(this屬性)并且尚未綁定,則的上下文 func 將是輸出函數(shù)在調(diào)用時(shí)具有的上下文。特別是,如果 if func 作為某些 objec 的方法被調(diào)用,并且 if func 尚未綁定,則為了保留上下文,建議將輸出函數(shù)的上下文也設(shè)置為該對(duì)象。

如果輸入函數(shù)以“節(jié)點(diǎn)樣式”調(diào)用其回調(diào)(即,回調(diào)的第一個(gè)參數(shù)是表示調(diào)用是否失敗的可選錯(cuò)誤參數(shù)),bindNodeCallback 則將提供方便的錯(cuò)誤處理,并且可能是更好的選擇。 bindCallback 會(huì)將此類函數(shù)與其他任何函數(shù)一樣對(duì)待,并且錯(cuò)誤參數(shù)(無論是否傳遞)將始終被解釋為常規(guī)回調(diào)參數(shù)。

例子

將 jQuery 的 getJSON 轉(zhuǎn)換為可觀察的 API

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));

接收傳遞給回調(diào)的參數(shù)數(shù)組

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'}]
});

比較有無異步 Scheduler 的行為

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!

在對(duì)象方法上使用 bindCallback

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
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為undefined。類型:SchedulerLike。

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

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

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

returns() => Observable<R1>

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

參量: 類型:
callbackFunc (callback: () => any) => any。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。 默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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。
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為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
調(diào)度器 可選的。默認(rèn)值為undefined。類型:SchedulerLike

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

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

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

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

也可以看看

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)