Using the File API without callbacks

So, it’s pretty much a given that working with the HTML5 API is a painful experience due to all those levels of callbacks. Even a simple operation turns into something unnecessarily complex, and so I present FileManager — a simple wrapper around frequently used portions of the File API that uses Promises (or Q.js, specifically).

It’s super easy to use, and while it’s officially part of the YASMF framework (, it doesn’t depend on it outright (you can use the gist instead: It does require that Q.js be loaded prior to using it — however you like doing that is up to you.

If you like to use Q.js/Promises, and use the File API a lot, you might just want to try it out.

As a quick example, here’s how it makes life a lot easier!

    var fm = new _y.FileManager(); // using YASMF
    fm.init ( fm.PERSISTENT, 1024 * 1024) // request the persistent file system with 1mb of space
         .then ( function () { return fm.readDirectoryContents ( ".", {} ); } ) // read the current directory
         .then ( function ( entries ) { //process the entries } ) // do something with the entries
         .catch ( function ( anError ) { console.log (anError.code); } ) // log the error code
         .done ();

If you want to use it in your projects, it works in the Google Chrome (v30 or higher) and PhoneGap / Cordova (dependent upon platform support).

Leave a Reply