Callbacks

Callback types

A callback is a function passed into another function to run later. Its type lists parameter types and the return type in one arrow shape. Writing the shape once keeps every caller and every handler in agreement.

type Done = (ok: boolean) => void; function run(done: Done): void { done(true); } run((ok) => { console.log(ok ? "done" : "failed"); });

done

Inline anonymous

Short one off handlers read best written inline at the call site. The arrow sits exactly where it runs. Extract the handler only when it grows or earns reuse.

const nums: number[] = [1, 2, 3]; nums.forEach((n) => { console.log(n); });

1

2

3

Named handlers

A named handler gives the callback a clear purpose and a testable home. Define it once with full annotations, then pass it by name. Stack traces and error messages also become far easier to read.

function handleNumber(n: number): void { console.log(n); } const nums: number[] = [1, 2, 3]; nums.forEach(handleNumber);

1

2

3

Flatten nesting

Nested callbacks pile indentation and hide the happy path. Named steps, early returns, and promise chains restore a flat top to bottom flow. Read the result as a list of stages, not a staircase.

function stepOne(): Promise<number> { return Promise.resolve(1); } stepOne() .then((n) => n + 1) .then((n) => { console.log(n); });

2

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a callback type such as (ok: boolean) => void describe?

    Answer

    A function that takes a boolean and returns nothing

    The arrow separates the parameter types from the return type, and every handler passed must match it.

  2. When is a named handler better than an inline arrow?

    Answer

    When the handler grows or is reused in several places

    A name gives the callback a purpose and a testable home, which a one off inline arrow does not need.

  3. What do named steps and promise chains do to deeply nested callbacks?

    Answer

    They restore a flat flow of stages

    Each stage appears once, in order, instead of indenting deeper with every call.

  4. What does this program print? type Done = (ok: boolean) => void; function run(done: Done): void { done(true); } run((ok) => { console.log(ok ? "done" : "failed"); });

    Answer

    done

    The function calls the handler with true, so the first branch prints.

  5. What does this program print? const nums: number[] = [1, 2, 3]; nums.forEach((n) => { console.log(n); });

    Answer

    1 / 2 / 3

    forEach calls the arrow once per item, and the output is 1, 2 and 3 on separate lines.

  6. What does this program print? function handleNumber(n: number): void { console.log(n); } const nums: number[] = [1, 2, 3]; nums.forEach(handleNumber);

    Answer

    1 / 2 / 3

    A named function is passed by name, and forEach calls it once per item, so the three lines print in order.

  7. What does this program print? function stepOne(): Promise<number> { return Promise.resolve(1); } stepOne() .then((n) => n + 1) .then((n) => { console.log(n); });

    Answer

    2

    The first stage adds one and the second prints the value it receives.

  8. What does the compiler say about this program? type Done = (ok: boolean) => void; function run(done: Done): void { done(true); } run((count: number) => { console.log(count); });

    Answer

    error TS2345: Argument of type '(count: number) => void' is not assignable to parameter of type 'Done'.

    The handler takes a number while the callback type promises a boolean parameter.

  9. What does the compiler say about this program? type Done = (ok: boolean) => void; function run(done: Done): void { done(); } run((ok) => console.log(ok));

    Answer

    error TS2554: Expected 1 arguments, but got 0.

    The callback type needs one boolean argument, and the call passes none.

  10. What does this program print? const nums = [1, 2, 3]; const total = nums.reduce((sum, n) => sum + n, 0); console.log(total);

    Answer

    6

    The callback adds each item to the running sum, which starts at zero.

Article author: Arthur Isaev