Writing async functions; try/catch; parallel vs sequential
Create async functions, await results, catch errors, and compare sequential vs parallel patterns.
Writing async functions; try/catch; parallel vs sequential is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro to async/await
Goal: Use async/await safely.
- Create async functions
- Await values
- try/catch errors
- Parallel vs sequential

Tiny Promise helper
Create a tiny Promise-based helper to test awaits.
// delay returns a Promise that resolves after ms
function delay(ms, value) {
return new Promise(function (resolve) {
setTimeout(function () { resolve(value); }, ms);
});
}
// Example usage
delay(10, "ok").then(function (v) {
console.log("delay:", v);
});

Async function basics
async marks a function that returns a Promise; inside it you can use await.
// An async function can use await inside
async function greet() {
const name = await delay(5, "Ayla");
console.log("Hello,", name);
}
// Call it
greet();

try/catch with await
Wrap awaits in try/catch when a step may reject.
// Simulate a failing task
function failAfter(ms, message) {
return new Promise(function (resolve, reject) {
setTimeout(function () { reject(new Error(message)); }, ms);
});
}
async function runSafe() {
try {
const v = await failAfter(5, "boom");
console.log("Value:", v);
} catch (e) {
console.log("Caught:", e.message);
}
}
runSafe();

Sequential pattern
Sequential: each await waits for the previous to finish; times add up.
// Sequential: total time ~ A + B
async function sequential() {
const a = await delay(8, "A");
const b = await delay(8, "B");
console.log("seq:", a + b);
}
sequential();

Parallel with all
Parallel: start tasks first, then await Promise.all; total time ~ max(A, B).
// Parallel: start both, then await together
async function parallel() {
const pa = delay(8, "A");
const pb = delay(8, "B");
const results = await Promise.all([pa, pb]);
console.log("par:", results.join(""));
}
parallel();

Parallel vs sequential quiz
Quick check: Faster pattern for independent tasks?

Recap
Recap: You wrote async functions, awaited values, handled errors with try/catch, and compared sequential awaits to faster parallel Promise.all.

Frequently asked questions
Is the “Writing async functions; try/catch; parallel vs sequential” lesson free?
Yes — the full text of “Writing async functions; try/catch; parallel vs sequential” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing async functions; try/catch; parallel vs sequential”?
Create async functions, await results, catch errors, and compare sequential vs parallel patterns. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Writing async functions; try/catch; parallel vs sequential” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing async functions; try/catch; parallel vs sequential
- Timeouts & Aborting with AbortController
- Retrying & Backoff Patterns