Skip to content

Commit a207917

Browse files
Fix getAsFileSystemHandle() example (mdn#40572)
* Fix getAsFileSystemHandle() example * Update files/en-us/web/api/datatransferitem/getasfilesystemhandle/index.md Co-authored-by: sideshowbarker <mike@w3.org> --------- Co-authored-by: sideshowbarker <mike@w3.org>
1 parent d495d8a commit a207917

File tree

1 file changed

+13
-9
lines changed
  • files/en-us/web/api/datatransferitem/getasfilesystemhandle

1 file changed

+13
-9
lines changed

files/en-us/web/api/datatransferitem/getasfilesystemhandle/index.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ None.
3939
This example uses the `getAsFileSystemHandle()` method to return
4040
{{domxref('FileSystemHandle', 'file handles', '', 'nocode')}} for dropped items.
4141

42+
> [!NOTE]
43+
> Because `getAsFileSystemHandle()` can only retrieve the entry handle in the same tick as the `drop` event handler, there must be no `await` before it. This is why we synchronously invoke `getAsFileSystemHandle()` for all items first, and then wait for their results concurrently.
44+
4245
```js
4346
elem.addEventListener("dragover", (e) => {
4447
// Prevent navigation.
@@ -47,17 +50,18 @@ elem.addEventListener("dragover", (e) => {
4750
elem.addEventListener("drop", async (e) => {
4851
// Prevent navigation.
4952
e.preventDefault();
53+
const handlesPromises = [...e.dataTransfer.items]
54+
// kind will be 'file' for file/directory entries.
55+
.filter((x) => x.kind === "file")
56+
.map((x) => x.getAsFileSystemHandle());
57+
const handles = await Promise.all(handlesPromises);
5058

5159
// Process all of the items.
52-
for (const item of e.dataTransfer.items) {
53-
// kind will be 'file' for file/directory entries.
54-
if (item.kind === "file") {
55-
const entry = await item.getAsFileSystemHandle();
56-
if (entry.kind === "file") {
57-
// run code for if entry is a file
58-
} else if (entry.kind === "directory") {
59-
// run code for is entry is a directory
60-
}
60+
for (const handle of handles) {
61+
if (handle.kind === "file") {
62+
// run code for if handle is a file
63+
} else if (handle.kind === "directory") {
64+
// run code for is handle is a directory
6165
}
6266
}
6367
});

0 commit comments

Comments
 (0)