GithubHelp home page GithubHelp logo

fs-batch's Introduction







fs-batch

Reads files asynchronously and groups the results into a single response.

(still buggy, not production ready)

Fs-batch aims at making it easier to work with asynchronous file reading in Node. You can specify an array of paths to read, and fs-batch will read all of them asynchronously, and then when each file has been read, it returns an array of results for you.

Example

var fsb = require('fs-batch');
fsb.readFiles(["path/to/file1", "file2", "path/file3"])
.success(function(files){
  console.log(files);
})

####How is this better than promises?
With promises, you can read files asynchronously, but only one at a time. Here is how a typical promise file reading goes:
NOT an example of fs-batch

promiseFs.readFile.("path/to/file1")
.then(function () { // won't run until file1 is read
    return promiseFs.readFile("file2");
})
.then(function () { // won't run until file1 AND file2 are read
    return promiseFs.readFile("file3");
})
//etc.

Unlike this example of a promise based library, fs-batch will read all of the files independently of eachother and then when they have all been read (or errored) then fs-batch will return them all.

####Response Methods

  • success() - return an array of succesfully found files
  • error() - returns an array of errors associated with file read

These methods act as promises
Example

var fsb = require('fs-batch');

fsb.readFiles(["path/to/file1", "file2", "path/file3"])
.success(function(files){
  console.log(files);
})
.error(function(errors){
   console.log(errors);
})

####The Returned Array of Files They returned array is made up of objects that each have 2 properties:

  • query - a string that is the same as the original used as the file path
  • data - the contents of the file

####Itterating the Returned Array of Files The array comes with a method called each() that allows you to run a function against each file.

Example

var fsb = require('fs-batch');

fsb.readFiles(["path/to/file1", "file2", "path/file3"])

.success(function(files){

  files.each(function(file){
    if(file.query === "path/file3"){
      console.log("file 3's contents: ", file.data);
    }
  })
  
})

fs-batch's People

Contributors

samueleaton avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.