Skip to main content

SKY-Q405 · Quality, structure and performance

Async Promise executor

High

new Promise(async (resolve, reject) => { ... }). The function passed to the Promise constructor (the executor) is async, so it returns a promise of its own that the constructor ignores.

Languages
TypeScript, JavaScript
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

When anything inside the executor throws, such as a missing file or bad JSON, the error rejects the ignored inner promise instead of the one the caller holds. The caller's promise never settles: await loadConfig() hangs forever, and the error shows up only as an unhandled rejection.

What Skylos flags

src/config.ts · TypeScriptReported on line 4
import { readFile } from "node:fs/promises"; export function loadConfig(path: string): Promise<Record<string, unknown>> {  return new Promise(async (resolve) => {    const text = await readFile(path, "utf8");    resolve(JSON.parse(text));  });}

What Skylos leaves alone

src/config.ts · TypeScript
import { readFile } from "node:fs"; export function loadConfig(path: string): Promise<Record<string, unknown>> {  return new Promise((resolve, reject) => {    readFile(path, "utf8", (err, text) => {      if (err) {        reject(err);        return;      }      resolve(JSON.parse(text));    });  });}

Why Skylos leaves it alone

The executor is an ordinary function that wraps a callback API, which is what the Promise constructor is for. Errors reach reject.

More languages

JavaScript

Flagged

src/ready.js · JavaScriptReported on line 2
export function waitForReady(db) {  return new Promise(async (resolve) => {    await db.connect();    resolve(db);  });}

Not flagged

src/ready.js · JavaScript
export function waitForReady(db) {  return new Promise((resolve, reject) => {    db.connect().then(() => resolve(db), reject);  });}

Why Skylos leaves it alone

The executor is not async; the connection's failure is passed to reject, so the caller sees it.

How Skylos decides

  1. Runs with --quality on .ts, .tsx, .js, .jsx, .mts, .cts, .mjs and .cjs files. Generated and minified files, and files that do not parse, are skipped.
  2. Finds new Promise(...) whose first argument is an async arrow function or function expression (type assertions and parentheses around it are looked through).
  3. Promise must be the built-in: Skylos skips the call when the file declares or reassigns its own Promise, anywhere a write could reach that call.
  4. Reports HIGH on the line of the new expression. The executor's body is not inspected.

How to fix it

Drop the constructor and write an async function. Errors then reject the returned promise as expected.

src/config.ts · TypeScript · the fix, against the flagged file
import { readFile } from "node:fs/promises"; Removed: export function loadConfig(path: string): Promise<Record<string, unknown>> {Removed:   return new Promise(async (resolve) => {Removed:     const text = await readFile(path, "utf8");Removed:     resolve(JSON.parse(text));Removed:   });Added: export async function loadConfig(path: string): Promise<Record<string, unknown>> {Added:   const text = await readFile(path, "utf8");Added:   return JSON.parse(text);}

When it can be wrong

  • An async executor that wraps all of its body in try/catch and calls reject is still reported, although it does settle. The plain async function is simpler there too.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of new Promise.

    TypeScript
    return new Promise(async (resolve, reject) => { // skylos: ignore[SKY-Q405]
  • [tool.skylos] ignore = ["SKY-Q405"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.