Skip to content
Node.js: Keep synchronous task failures in a batch report

Node.js: Keep synchronous task failures in a batch report

November 15, 2026

A batch runner may accept functions that return values, return promises or throw before returning. Calling those functions directly while building the promise array lets a synchronous exception interrupt the batch. Schedule each invocation inside a promise callback so it becomes one reportable outcome.

Set up

Tested on Linux with Node.js v24.22.1. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No package installation or network access is needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as node-settle-task-functions.mjs:

import assert from 'node:assert/strict';

const tasks = [
  { id: 'summary', run: () => 'ready' },
  { id: 'config', run: () => { throw new Error('invalid config'); } },
  { id: 'image', run: () => Promise.reject('not found') },
  { id: 'index', run: async () => 'indexed' },
];

const pending = tasks.map((task) => Promise.resolve().then(() => task.run()));
const settled = await Promise.allSettled(pending);
const report = settled.map((result, index) => ({ id: tasks[index].id, ...result }));

assert.deepEqual(report.map(({ status }) => status), [
  'fulfilled', 'rejected', 'rejected', 'fulfilled',
]);
assert.equal(report[1].reason.message, 'invalid config');
assert.equal(report[2].reason, 'not found');
assert.equal(report[3].value, 'indexed');

for (const result of report) {
  if (result.status === 'fulfilled') {
    console.log(`${result.id}: ok (${result.value})`);
  } else {
    const reason = result.reason instanceof Error
      ? result.reason.message : String(result.reason);
    console.log(`${result.id}: failed (${reason})`);
  }
}

Run:

node node-settle-task-functions.mjs

Observed output:

summary: ok (ready)
config: failed (invalid config)
image: failed (not found)
index: ok (indexed)

Read the result

The callback converts the configuration task’s thrown error into a rejected promise. The image task already returns a rejected promise. Both appear in the report, and the final task still runs.

The result array preserves input order, so each record can be paired with the corresponding task ID. Keep the task array unchanged until the report is assembled. Rejection reasons need not be Error objects; this example formats its known string reason explicitly.

Apply it carefully

This wrapper schedules every task without limiting concurrency. It adds no timeout or cancellation, and a task that never settles keeps the batch pending. CPU-heavy functions still block the JavaScript thread while running. Use a separate bounded scheduler for large batches, and pass cancellation signals to operations that support them.

The ECMAScript allSettled algorithm specifies the ordered outcome records. Its promise reaction algorithm turns a callback’s abrupt completion into rejection.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- node-settle-task-functions.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on