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.

Workflow

  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

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

Explore all resources