Skip to main content

Javascript Promises, explained.

Published 2021/03/23 13:57

Promises in Javascript are a way of isolating something that will likely take a while to complete. Like getting content from another website, or a file

See this code below:

var hello = "Hello world"
console.log(hello)
var number = 5
var numberSquared = 5 * 5
console.log(numberSquared)

You would expect each line to be executed line by line. First, "Hello world" is stored in the "hello" variable, THEN it gets printed, THEN the number variable gets assigned to number 5, THEN it gets squared and FINALLY, the number 25 gets printed in the console. Makes sense!

What if there's something that takes a bit longer to complete? Like getting data from an API? In languages like Python, you'd expect your program to freeze until it's completed!

See the example Python code below:

print("Getting info...")
data = requests.get("https://api.example.com/data")
print("Data retrieval complete!")
print(data)

What you'd expect is that it will print "Getting info…", then freeze until the data from the API is retrieved, and once that's done, print "Data retrieval complete" + data.

And that makes sense if you're running a program from a Terminal. It isn't that confusing to your user.

Javascript however, is different. Javascript is made for the browser. If something goes wrong, it can affect the website, which in turn will confuse your visitors. Not something you want!

What I mean is that if you made a request to an API with Javascript and your script gets stuck while it receives the data, your browser gets stuck as well. Buttons stop working, content stops loading, interactions grind to a halt until the API decides gives the data back.

This is where Promises come in to save the day…

Promises is essentially a way of saying:

"Go get some data from this API. However, I'm not gonna sit here and wait for you because that could take a while and it would freeze my browser"

Promises is also a way of saying:

"Go get some data from this API. I'm going to move on to the next line, BUT once you've got some data back, I want you to do THIS with them" Example:

axios.get("https://api.example.com/data")
    .then((response) => {
        console.log(response.data)
        console.log("All done!")
    })

console.log("JS is great!")

What the example above essentially does:

  • print "Getting some data for you…"
  • print "JS is great"
  • (once the data is back) print the data and then print "All done!"

That way your browser doesn't freeze while fetching data. So Javascript is happy, your browser is happy, and (most importantly) your user is happy.

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik