What is a Promise?

You'll probably be wondering by now what it is that I'm calling a Promise? Well, a Promise, as it sounds, is a Promise made by JavaScript that the asynchronous function will complete execution at some point.

In the previous chapter, we came across an asynchronous event: reading the contents of a file using FileReader. This is how FileReader worked:

  • It starts reading the file. Since reading is an asynchronous event, other JavaScript code will continue execution while reading is still happening.

You might be wondering, what if I need to execute some code only after the event is complete? This is how FileReader handles it:

  • Once the reading is complete, FileReader fires a load event
  • It also has an onload() method that listens ...

Get JavaScript by Example 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.