floating-rejection
An async call that can reject with a PropagateError is neither awaited, returned, nor .catch()ed.
Why: the rejection happens after the calling code has moved on, so no try/catch can catch it, not even one directly around the call. It surfaces as an unhandled rejection. Guide: Async.
✕ Rejectedfloating-rejection
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Timeout extends PropagateError {}
async function ping(host: string): Promise<number> {
if (host === "") throw new Timeout("no host");
return 1;
}
export function check(host: string): void {
ping(host);
}✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Timeout extends PropagateError {}
async function ping(host: string): Promise<number> {
if (host === "") throw new Timeout("no host");
return 1;
}
// Await it (the error now propagates to this function's callers),
export async function check(host: string): Promise<number> {
return await ping(host);
}
// or handle the rejection where you start it: the top of this background job.
export function checkInBackground(host: string): void {
ping(host).catch((error: unknown) => console.error(error));
}A function doesn't need the async keyword to hand back a rejection. One that returns another async call's promise (also through .then(...) or .finally(...)), or a new Promise(...) whose executor throws, is treated the same way:
✕ Rejectedfloating-rejection
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Timeout extends PropagateError {}
async function ping(host: string): Promise<number> {
if (host === "") throw new Timeout("no host");
return 1;
}
function pingTwice(host: string): Promise<number> {
return ping(host).then((first) => first + 1);
}
export function check(host: string): void {
try {
pingTwice(host);
} catch {
console.error("never runs: the promise rejects after this try has finished");
}
}What such a function throws before it makes the promise is still thrown: a try around the call catches that part, .catch(...) only the rejection.
A rejection written as a call is a throw by another name: return Promise.reject(new X()), and reject(new X()) in a new Promise executor (also from a callback inside it, such as a timer's or a listener's, and the reject of Promise.withResolvers()), reject the function's promise with X the way throw new X() in an async function does. A Promise.reject(...) that nothing awaits, returns or handles is reported where it is written:
✕ Rejectedfloating-rejection
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Timeout extends PropagateError {}
function ping(host: string): Promise<number> {
return new Promise((resolve, reject) => {
setTimeout(() => (host === "" ? reject(new Timeout("no host")) : resolve(1)), 100);
});
}
export function check(host: string): void {
ping(host);
}Quick fixes: Add await (in async functions) · Handle the rejection with .catch(…).
A dropped call that can't reject is floating-promise's (a warning): the two never report the same call.
A promise kept in a local const (or a let only ever set to such calls) is followed to where it is used: await p, return p, p.catch(...), Promise.all([p, q]), or handing it to one of your own functions whose parameter is awaited, returned or handled on every path (settle(p) with async function settle(p) { try { return await p; } catch ... }). Its rejection is handled there, or goes on from there, when every path from the call reaches such a use first. A path that leaves before it (return, throw, break), awaits something else in between (which may reject first), or uses it only in a branch, leaves it floating. A typeof p === "string" test sends it on only on the side where it can be a promise.
✅ Accepted: every path awaits the charge, or hands it to a function that awaits it in a try. A declined card ends the checkout request, which answers 402.
✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Declined extends PropagateError {}
type Reply = { readonly status: number; readonly body: string };
const declined = (): Reply => ({ status: 402, body: "card declined" });
async function charge(order: string): Promise<string> {
if (order === "") throw new Declined("card declined");
return order;
}
function receipt(order: string): string | Promise<string> {
if (order === "cached") return "r-1";
return charge(order);
}
async function settle(pending: Promise<string>): Promise<Reply> {
try {
return { status: 200, body: await pending };
} catch (e) {
if (e instanceof Declined) return declined();
throw e;
}
}
export function checkout(order: string): Reply | Promise<Reply> {
let answer: string | Promise<string>;
try {
answer = receipt(order);
} catch (e) {
if (e instanceof Declined) return declined();
throw e;
}
if (typeof answer === "string") return { status: 200, body: answer };
return settle(answer);
}❌ Rejected: on the free path nobody awaits the charge
✕ Rejectedfloating-rejection
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Declined extends PropagateError {}
async function charge(order: string): Promise<string> {
if (order === "") throw new Declined("card declined");
return order;
}
export async function pay(order: string, free: boolean): Promise<string> {
const pending = charge(order);
if (free) return "free";
return await pending;
}What the rule can't see: where a promise goes once it is stored in a field or an array, or passed to a package's function or one of yours that does anything else with it: it is reported as if it floated. A promise's reject is followed where the executor or Promise.withResolvers() hands it out, not once it is stored and called through what holds it (pending.get(id)?.reject(e)); handing it on by name is no-throw. A call between making the promise and awaiting it that throws synchronously isn't counted as leaving. A .then(...) callback counts as running detached, so an error thrown inside it is reported there.