FileSystemDirectoryEntry: createReader() method

The FileSystemDirectoryEntry interface's method createReader() returns a FileSystemDirectoryReader object which can be used to read the entries in the directory.

Syntax

js
createReader()

Parameters

None.

Return value

A FileSystemDirectoryReader object which can be used to read the directory's entries.

Examples

This example creates an async function called readDirectory(), which fetches all of the entries in the specified FileSystemDirectoryEntry and returns them in an array.

js
async function readDirectory(directory) {
  const dirReader = directory.createReader();
  const entries = [];

  while (true) {
    const results = await new Promise((resolve, reject) => {
      dirReader.readEntries(resolve, reject);
    });

    if (!results.length) {
      break;
    }

    for (const entry of results) {
      entries.push(entry);
    }
  }

  return entries;
}

This works by calling readEntries() repetitively to get all the entries in the directory, concatenating each batch to the array. When it returns an empty array, all entries have been read, and the loop ends.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android
createReader 13 79 50 No 11.1 18 50 14 11.3 1.0 ≤37

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader