Prompt Engineering Refactoring JavaScript

Modernize JavaScript Code — From Callbacks to Async/Await Safely

var to const, callback pyramids to async/await, || guards to ?? — with the trap called out by name: || treats 0 and empty string as missing, ?? does not.

Overview

JavaScript modernization has a famous failure mode: the mechanical swap that changes semantics. Replacing a || fallback with ?? changes what happens for 0 and empty string; flattening a callback pyramid reorders error handling; var-to-const surfaces hoisting assumptions. This prompt sets the JavaScript modernization scope — const/let over var, async/await over callback pyramids, optional chaining and nullish coalescing with the ||-vs-?? trap stated inline, destructuring and array methods where they clarify — and loads a real callback-style module whose code carries exactly that trap: a `withAvatar || user` fallback the contract must treat as semantics, not syntax.

How to use this resource

  1. Carry the code verbatim

    The module rides in a fenced block — the contract transforms what exists, not a paraphrase of it.

  2. Flatten with care

    Callback pyramids become async/await with error paths preserved — every callback(err) branch accounted for.

  3. Treat || as semantics

    Each || fallback is checked: does this code mean "missing" or "falsy"? Only then does ?? apply.

Why This Works

  • The ||-vs-?? trap stated inline prevents the most common modernization regression
  • Error-path preservation keeps callback-to-async conversions honest
  • A fenced, verbatim module grounds the transformation in real code

Best for

  • Node and browser code written in the callback era
  • Modules mixing promise styles inconsistently
  • Codebases adopting modern syntax file by file

Not for

  • Debugging why the callback code fails — that's the Debugging Prompt Generator
  • Generating tests for the modernized module — that's the Test Case Prompt Generator

Use cases

  • Converting callback-style modules to async/await
  • Eliminating var and its hoisting behavior
  • Replacing && and || guards with ?. and ?? — where semantics allow

FAQ

Does this prompt handle the trap where replacing || with ?? changes behavior?

That trap is called out inline: the modernization scope says "careful: || treats 0 and \"\" as missing, ?? does not," and the loaded module carries a real withAvatar || user fallback the contract must treat as semantics, not syntax. Each || is checked for "missing" versus "falsy" before ?? applies. The Refactor Prompt Builder generates this prompt; the modernization runs in your assistant, and you verify it.

What happens if a safe modernization and keeping behavior identical conflict?

The prompt resolves it one way: "If achieving the goal and preserving behavior conflict, behavior wins: stop and explain the conflict." Borderline-safe transformations get the conservative version applied with the aggressive option noted, and anything skipped for safety lands under a "Suggested but not applied" list with what info would unlock it. It's an instruction the model should follow; you still review the flagged changes and run your own tests.

More resources from Refactor Prompt Builder

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.