In-session live coding for Week 3 (Advanced JavaScript). The demo walks through fetch with JSONPlaceholder, async/await, consuming promises with .then() / .catch(), creating promises with new Promise, try / catch with async code, Promise.all, and an optional promise microtask loop. You implement the worksheet during class; the solution file is the finished version.
| File | Purpose |
|---|---|
| index.js | Worksheet: section banners, // Task: lines, and // Next: hints. Only getUser and promiseLoop are declared; you add the rest while teaching. Use this file when leading the session. |
| index-solution.js | Full implementation: showOutput, getUser, promise consumption, timed and pizza promises, try / catch fetch, Promise.all, and promiseLoop. |
| index.html | Minimal page that loads index.js. Add markup (e.g. <pre id="out">) when you want on-page output; the solution’s showOutput writes to #out. |
| style.css | Basic layout and styles for #out (and main if you use it). |
Everything lives in index.js. Search for // ========== for section breaks, // Task: for what to build, and // Next: for suggested links to the trainee exercises.
USER_URL–https://jsonplaceholder.typicode.com/users/1POST_URL–https://jsonplaceholder.typicode.com/posts/1TODO_URL–https://jsonplaceholder.typicode.com/todos/1(optional extra)
showOutput(text)– SetstextContenton#outwhen that element exists.getUser()–asyncfetchofUSER_URL, then.json(), thenshowOutputwith stringified user data.loadOneResourceWithThen()– Same resource with.then/.catchonly (noasync/await).oneSecondMessage()– Promise that resolves after one second, then shows"It worked".demoOrderPizza()– Delayed resolve or reject, then shows pizza or error text.getUserWithTryCatch()– Same fetch pattern asgetUserwithtry/catchand errors on the page.demoPromiseAll()– FetchesUSER_URLandPOST_URLin parallel, then shows a short two-line summary.promiseLoop()– Schedules endless microtasks (illustration only; can freeze the tab if called).
Not implemented in these files; use the session plan (e.g. npm q or your own example on the board).