Pass a callback that updates response state.

callback-dispatch A callback lets dispatch code hand a normalized value to another function. The replay shows the callback body as the dispatched work.

Callback Dispatch

mode
callback_dispatch.js
Replay: real traced execution (multi-file project)
const mode = "status";
let access = "";
let response = "";

function dispatch(command, callback) {
  const normalized = command.trim().toLowerCase();
  callback(normalized);
}

dispatch(mode, (command) => {
  if (command === "archive") {
    access = "review";
  } else {
    access = "ok";
  }
  response = "handled:" + command;
});

console.log("mode=" + mode);
console.log("access=" + access);
console.log(response);
const mode = "refresh";
let access = "";
let response = "";

function dispatch(command, callback) {
  const normalized = command.trim().toLowerCase();
  callback(normalized);
}

dispatch(mode, (command) => {
  if (command === "archive") {
    access = "review";
  } else {
    access = "ok";
  }
  response = "handled:" + command;
});

console.log("mode=" + mode);
console.log("access=" + access);
console.log(response);
const mode = "archive";
let access = "";
let response = "";

function dispatch(command, callback) {
  const normalized = command.trim().toLowerCase();
  callback(normalized);
}

dispatch(mode, (command) => {
  if (command === "archive") {
    access = "review";
  } else {
    access = "ok";
  }
  response = "handled:" + command;
});

console.log("mode=" + mode);
console.log("access=" + access);
console.log(response);
  1. mode ← status, access ← (empty), response ← (empty)

    1const mode→ status = "status"; //@mode="refresh", "archive"2let access→ (empty) = "";3let response→ (empty) = "";45function dispatch(command, callback) {6  const normalized = command.trim().toLowerCase();7  callback(normalized);8}910dispatch(modestatus, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});
  2. normalized ← status

    2let access = "";3let response = "";45function dispatch(commandstatus, callback(command) => {
        if (command === "archive") {
            access = "review";
        }
        else {
            access = "ok";
        }
        response = "handled:" + command;
    }) {6  const normalized→ status = commandstatus.trim().toLowerCase();7  callback(normalizedstatus);8}
  3. dispatch(mode, (command) =>

    7  callback(normalized);8}910dispatch(modestatus, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});1819console.log("mode=" + modestatus);20console.log("access=" + accessok);21console.log(responsehandled:status);
    outputmode=status
    access=ok
    handled:status
  1. mode ← refresh, access ← (empty), response ← (empty)

    1const mode→ refresh = "refresh";2let access→ (empty) = "";3let response→ (empty) = "";45function dispatch(command, callback) {6  const normalized = command.trim().toLowerCase();7  callback(normalized);8}910dispatch(moderefresh, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});
  2. normalized ← refresh

    2let access = "";3let response = "";45function dispatch(commandrefresh, callback(command) => {
        if (command === "archive") {
            access = "review";
        }
        else {
            access = "ok";
        }
        response = "handled:" + command;
    }) {6  const normalized→ refresh = commandrefresh.trim().toLowerCase();7  callback(normalizedrefresh);8}
  3. dispatch(mode, (command) =>

    7  callback(normalized);8}910dispatch(moderefresh, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});1819console.log("mode=" + moderefresh);20console.log("access=" + accessok);21console.log(responsehandled:refresh);
    outputmode=refresh
    access=ok
    handled:refresh
  1. mode ← archive, access ← (empty), response ← (empty)

    1const mode→ archive = "archive";2let access→ (empty) = "";3let response→ (empty) = "";45function dispatch(command, callback) {6  const normalized = command.trim().toLowerCase();7  callback(normalized);8}910dispatch(modearchive, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});
  2. normalized ← archive

    2let access = "";3let response = "";45function dispatch(commandarchive, callback(command) => {
        if (command === "archive") {
            access = "review";
        }
        else {
            access = "ok";
        }
        response = "handled:" + command;
    }) {6  const normalized→ archive = commandarchive.trim().toLowerCase();7  callback(normalizedarchive);8}
  3. dispatch(mode, (command) =>

    7  callback(normalized);8}910dispatch(modearchive, (command) => {11  if (command === "archive") {12    access = "review";13  } else {14    access = "ok";15  }16  response = "handled:" + command;17});1819console.log("mode=" + modearchive);20console.log("access=" + accessreview);21console.log(responsehandled:archive);
    outputmode=archive
    access=review
    handled:archive