Last Updated: February 25, 2016
·
2.146K
· kerrishotts

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 (https://github.com/photokandyStudios/YASMF-Next), it doesn't depend on it outright (you can use the gist instead: https://gist.github.com/kerrishotts/7147214). 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.

Docs: https://github.com/photokandyStudios/YASMF-Next/wiki/FileManager

Github: https://github.com/photokandyStudios/YASMF-Next/blob/master/lib/yasmf/util/fileManager.js

Gist (stand-alone): https://gist.github.com/kerrishotts/7147214

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).

1 Response
Add your response

If you want an ugly example that works in Chrome v30+: http://jsbin.com/UvAtiha/2

over 1 year ago ·