Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active December 23, 2015 04:29
Show Gist options
  • Select an option

  • Save mattpodwysocki/6580384 to your computer and use it in GitHub Desktop.

Select an option

Save mattpodwysocki/6580384 to your computer and use it in GitHub Desktop.
Examples of using the Node.js bindings for callback handling
var Rx = require('rx');
var fs = require('fs');
function allExists (files) {
return Rx.Observable.for(files,
function (file) {
return Rx.Node.fromCallback(fs.exists)(file);
}
)
.all();
}
var allFilesExist = allExists(['rx.js', 'rx.node.js'])
.subscribe(function (x) { console.log('All exist? ' + x); });
// => All exist? true
var Rx = require('rx');
var fs = require('fs');
function renameAll (files) {
return Rx.Observable.for(files,
function (file) {
return Rx.Node.fromNodeCallback(fs.rename)(file.oldName, file.newName);
}
)
.ignoreElements();
}
var files = [
{oldName: 'rx.js', newName: 'rx.old.js'},
{oldName: 'rx.node.js', newName: 'rx.node.old.js' }
];
var allFilesExist = renameAll(files)
.subscribe(
null, /* No onNext required */
function (err) { console.log('Error renaming files: ' + err); },
function (x) { console.log('All files renamed'); });
// => All files renamed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment