Chapter 8. The Synchronous API
Introduction
The HTML5 Filesystem API includes a synchronous version that, for the most part, is exactly the same as its asynchronous cousin. The methods, properties, features, and functionality will be familiar. The major deviations are:
The synchronous API can only be used within a Web Worker context. The asynchronous API can be used in and out of a worker.
Callbacks are out. API methods now return values.
The global methods on the
window
object (requestFileSystem()
andresolveLocalFileSystemURL()
) are renamed asrequestFileSystemSync()
andresolveLocalFileSystemSyncURL()
and members of the worker’s global scope.
The two APIs are the same, save these few exceptions. Because there is not much new to cover, this chapter won’t cover the synchronous API in great detail. I’ll only highlight the exceptions to the asynchronous API and provide a few examples to make things clear.
Opening a Filesystem
A web application obtains access to the synchronous filesystem by
requesting a LocalFileSystemSync
object
from within a web worker. The requestFileSystemSync()
is exposed to the
worker’s global scope:
var fs = requestFileSystemSync(TEMPORARY
,1024*1024
/*1MB*/);
Warning
This method is currently vendor prefixed as webkitRequestFileSystemSync
.
The reader should note two things about this call: the new return value and the absence of success and error callbacks.
Working with Files and Directories
The synchronous filesystem has a getFile()
and getDirectory()
which return a ...
Get Using the HTML5 Filesystem API now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.