RxJS bindCallback

2020-10-13 18:32 更新

将回调 API 转换为返回 Observable 的函数。

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

参量

callbackFunc 类型:Function
resultSelector 可选的。默认值为 undefined。类型:Function | SchedulerLike
调度器 可选的。默认值为 undefined。用于调度回调的调度程序。

returns

(...args: any[]) => Observable<T>:返回 Observable 的函数,该函数提供与回调传递的值相同的值。

描述

给它一个 f类型 f(x, callback)的函数 g,它将返回一个函数,当调用 as 时 g(x)将输出一个 Observable。

bindCallback 不是运算符,因为其输入和输出都不是可观察的。输入是 func 带有某些参数的函数。最后一个参数必须 func 是完成后调用的回调函数。

输出的 bindCallbackfunc 与最后一个参数(回调)相同的,与相同的参数的函数。使用参数调用输出函数时,它将返回一个 Observable。如果函数 func 使用一个参数调用其回调,则 Observable 将发出该值。另一方面,如果使用多个值调用回调,则产生的 Observable 将发出一个以所述值作为参数的数组。

这是非常重要的是要记住,输入功能 func,当输出功能是不叫,而是当由输出函数返回的可观测订阅。这意味着,如果 func 发出 AJAX 请求,则每次有人订阅生成的 Observable 时都会发出该请求,但不是之前。

最后一个可选参数-- scheduler可用于控制 func在有人订阅 Observable 之后何时进行调用,以及何时传递传递给回调的结果。默认情况下,对 Observable 的预订是 func 同步进行的,但是将其 async用作最后一个参数会将调用推迟到 func,就像将调用包装成 will setTimeout超时0一样。如果要使用异步调度程序并 subscribe在输出 Observable 上调用,则当前正在执行的所有函数调用将在 func调用之前结束。

默认情况下,传递给回调的结果在 func调用回调后立即发出。特别是,如果回调是同步调用的,则结果Observable 的订阅也将 next 同步调用该函数。如果您想推迟该呼叫,则可以 async像以前一样使用。这意味着通过使用 Scheduler.async 您可以确保 func 始终异步调用其回调,从而避免使 Zalgo 感到恐惧。

请注意,由输出函数创建的 Observable 将始终发出单个值,然后立即完成。如果 func 多次调用回调,则后续调用中的值将不会出现在流中。如果您需要收听多个呼叫,则可能要使用 fromEventfromEventPattern 代替。

如果 func 依赖于某些上下文(this属性)并且尚未绑定,则的上下文 func 将是输出函数在调用时具有的上下文。特别是,如果 if func 作为某些 objec 的方法被调用,并且 if func 尚未绑定,则为了保留上下文,建议将输出函数的上下文也设置为该对象。

如果输入函数以“节点样式”调用其回调(即,回调的第一个参数是表示调用是否失败的可选错误参数),bindNodeCallback 则将提供方便的错误处理,并且可能是更好的选择。 bindCallback 会将此类函数与其他任何函数一样对待,并且错误参数(无论是否传递)将始终被解释为常规回调参数。

例子

将 jQuery 的 getJSON 转换为可观察的 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));

接收传递给回调的参数数组

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!

在对象方法上使用 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
调度器 可选的。默认值为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>

也可以看看

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号