Last active
December 23, 2015 04:29
-
-
Save mattpodwysocki/6580384 to your computer and use it in GitHub Desktop.
Examples of using the Node.js bindings for callback handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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