RxJS bindNodeCallback
将Node.js样式的回调API转换为返回Observable的函数。
bindNodeCallback<T>(callbackFunc: Function, resultSelector: Function | SchedulerLike, scheduler?: SchedulerLike): (...args: any[]) => Observable<T>
参量
callbackFunc | 类型:Function 。 |
---|---|
resultSelector | 类型:Function | SchedulerLike 。 |
调度器 | 可选的。默认值为undefined 。用于调度回调的调度程序。 |
returns
(...args: any[]) => Observable<T>
:返回Observable的函数,该函数传递与Node.js回调传递的值相同的值。
描述
就像 bindCallback
,但是回调应该是 type callback(error, result)
。
bindNodeCallback
不是运算符,因为其输入和输出都不是可观察的。输入是 func
带有某些参数的函数,但最后一个参数必须 func
是完成后调用的回调函数。回调函数应遵循 Node.js 约定,其中回调的第一个参数是错误对象,表示调用是否成功。如果将该对象传递给回调,则表示出了点问题。
输出的 bindNodeCallback
是 func
与最后一个参数(回调)相同的,与相同的参数的函数。当使用参数调用输出函数时,它将返回一个 Observable。如果 func
使用错误参数调用其回调,则 Observable 也将使用该值发生错误。如果未传递错误参数,则 Observable 将发出第二个参数。如果有更多参数(第三个,依此类推), Observable 将发出一个数组,其中包含除第一个错误参数以外的所有参数。
请注意, func
不会同时调用输出函数,而是在订阅结果 Observable 时调用。默认情况下,对的调用 func
将在订阅后同步发生,但是可以通过将其 scheduler
作为可选的第三个参数适当提供来进行更改。SchedulerLike
还可以控制 Observable 何时发出回调中的值。要了解更多信息,请查阅的文档 bindCallback
,其 SchedulerLike
工作原理完全相同。
与中一样 bindCallback
,this
输入函数的上下文(属性)在被调用时将被设置为返回函数的上下文。
Observable 发出值后,它将立即完成。这意味着即使 func
调用再次回调,来自第二次和连续调用的值也将永远不会出现在流中。如果您需要处理多次调用回调的函数,请签出 fromEvent
或 fromEventPattern
代替。
请注意,它 bindNodeCallback
也可以在非 Node.js 环境中使用。“ Node.js样式”回调只是一个约定,因此,如果您是为浏览器或任何其他环境编写的,并且您使用的 API 实现了该回调样式,那么 bindNodeCallback
也可以在该 API 函数上安全地使用它。
请记住,传递给回调的 Error 对象不必是 JavaScript 内置 Error
对象的实例。实际上,它甚至不需要对象。当回调函数的错误参数的值为真时,将其解释为“存在”。例如,它可以是非零数字,非空字符串或 boolean true
。在所有这些情况下,所得的 Observable 均会因该值而出错。这意味着通常的常规样式回调在 bindNodeCallback
使用时会经常失败 。如果您的 Observable 错误发生的次数比您预期的要多得多,请检查是否确实以 Node.js 样式调用了回调,如果不是,则切换为 bindCallback
。
请注意,即使 error 参数在技术上存在于回调中,但其值是错误的,它仍然不会出现在 Observable 发出的数组中。
例子
从文件系统读取文件并以 Observable 形式获取数据
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 。调度器可选的。默认值为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 调度器可选的。默认值为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 。 调度器可选的。默认值为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 。 调度器可选的。默认值为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 。调度器可选的。默认值为undefined 。类型:SchedulerLike 。 |
returns() => Observable<R1>
bindNodeCallback(callbackFunc: (callback: (err: any) => any) => any, scheduler?: SchedulerLike): () => Observable<void>
参量: | 类型: |
---|---|
callbackFunc | (callback: (err: any) => any) => any 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。 调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为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 。调度器可选的。默认值为undefined 。类型:SchedulerLike |
returns(...args: any[]) => Observable<any[]>