Skip to content

#304 Reject Callback

Adam Kubiś edited this page Oct 30, 2026 · 2 revisions

304 Reject Callback

Reject the promise with an Error whose message is "It's not a dog!".

The original exercise requests a primitive string as the rejection reason. This version deliberately modernizes that detail: an Error preserves the same message and adds a stack trace. The assertions verify both the error type and the original message.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/304 Reject Callback.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Reject the promise with an Error whose message is "It's not a dog!".
//The original exercise requests a string; this version preserves the message and adds an error stack.
import { setTimeout } from "node:timers";
const promise = new Promise((_resolve, reject) => {
    setTimeout(() => {
        reject(new Error("It's not a dog!"));
    }, 1000);
});
import { assertEquals } from "../testHelper.js";

assertEquals(promise instanceof Promise, true);

const rejection = await promise.then(
    () => {
        throw new Error("Expected the promise to reject.");
    },
    reason => reason
);

assertEquals(rejection instanceof Error, true);
assertEquals(rejection.message, "It's not a dog!");
assertEquals(typeof rejection.stack, "string");

Clone this wiki locally