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)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay