DEV Community

Rémy F.
Rémy F.

Posted on

Async Promise-based FileReader one-liner

The Line

const cat = (f) =>
  new Promise(resolve => Object.assign(new FileReader(), {onload(){resolve(this.result)}}).readAsText(f));
Enter fullscreen mode Exit fullscreen mode

Usage

<input type="file" onchange="cat(files[0]).then(console.log)">
Enter fullscreen mode Exit fullscreen mode

Top comments (0)