fromCallback

Rx.Node.fromCallback(func, [scheduler], [context])

#

Deprecated in favor of Rx.Observable.fromCallback in rx.async.js.

Converts a callback function to an observable sequence.

参数

  1. func (Function): Callback function
  2. [scheduler = Rx.Scheduler.timeout] (Scheduler): Scheduler used to execute the callback.
  3. [context] (Any): The context to execute the callback.

返回值

(Function): Function, when called with arguments, creates an Observable sequence from the callback.

var fs = require('fs');
var Rx = require('Rx');

// Wrap exists
var exists = Rx.Node.fromCallback(fs.exists);

// Call exists
var source = exists('/etc/passwd');

var observer = Rx.Observer.create(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);   
    },
    function () {
        console.log('Completed');   
    }
);

var subscription = source.subscribe(observer);

// => Next: true
// => Completed