×

javascript · level 3 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A promise is an IOU: now, later.

fetch hands back a promise: an IOU for a value that is not ready yet. A promise starts pending. Later it is settled, exactly once: it either resolves with a value or rejects with an error. await pauses until it settles.

runs task after ms milliseconds, thousandths of a second. Put it inside and you have made your own promise. Call resolve(value) when the work succeeds or reject(error) when it fails.

function bakeCake(flavour) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (flavour === "") {
        reject(new Error("No flavour chosen"));
      } else {
        resolve(`${flavour} cake`);
      }
    }, 100);
  });
}
console.log("Oven on");
const cake = bakeCake("Lemon");
console.log(await cake);

Gwecode lets you write await at the top level of your program, outside any function.