For the complete documentation index, see llms.txt. This page is also available as Markdown.

Session Plan

Session Materials

These are some examples of previously created materials by mentors that you can use yourself, or for inspiration.

  • Notion Page Handout (by Thomas)

  • Demo – In-session live coding from Code inspiration below (not the trainee exercises). index.js = worksheet stubs; index-solution.js = reference. README.

  • Promises chaining diagram (PDF) – Hand-drawn sketch you can project or redraw on the board when explaining how .then() chains.

Session Outline

Promises is notoriously difficult to teach! I teach consumption and creation of promises totally separate! And show them that it's just like with functions. There is a creation part and a consumption part.

First when they fully understand one part of promises, I move on! Don't over-complicate things. Only mention the resolve function to begin with. When they get that, say that there also is a reject function. Take as many baby steps as is possible! Don't mention that resolve and reject can take an argument to begin with, first later explain that.

  • Async/await - simple introduction focused on usage

    • Quickly recap asynchronicity

      • Ask the trainees what it means that some code is asynchronous

    • Practical example of async/await

    • Exercises 1

  • Promise

    • Why do we use promises?

    • Consumption

      • Code inspiration

      • Example, call some function that returns a promise (like fetch)

      • Exercises 2

    • Creation

    • Async await

    • Promise.all - Let trainees investigate

    • Optional - Chaining. Calling .then returns a promise. Only get to here when they understand async/await and promise consumption and creation.

      • I found that drawing/demoing how it works under the hood useful when explaining how promises feed into the next .then(). You can find example in Session matherials.

      • Reason for promise

    • Exercises 5 and Exercises 6

Exercises

See Exercises. Trainees show results on the page (update the DOM), not in the console.

Console order – Optional “what is logged?” promise chaining.

Code inspiration

Async/await - simple usage

Promise consumption

So how did the async/await example above actually work? Let's get into promises!

If you have a promise, you can call two functions on that promise. '.then' and '.catch'. When are these functions called? What does it mean that a promise is resolved or rejected?

The trainees should be able to answer these questions: // Question 1: What does it mean that a promise is resolved? Which method on a promise get called? // Question 2: What does it mean that a promise is rejected? Which method on a promise get called? // How would you explain your mom what resolved and rejected means?

Promise creation

When you create a new promise you give it a function that has two functions as parameters (resolve and reject). Resolve is called when everything in a promise goes well. Reject is called when something goes wrong.

Back to async/await

So writing async in front of a function makes it return a promise! The keyword await makes JavaScript wait until that promise resolved and returns its result.

Function that returns a promise

Last updated