Search⌘ K
AI Features

Solution: Bridge Callback and Promise APIs

Understand how to implement an adapter that bridges callback and promise APIs, allowing modern async/await calls to interact with legacy callback-style Node.js database clients. This lesson helps you create maintainable, clean code by standardizing interfaces across different asynchronous styles.

Solution explanation

  • Lines 2–12: We simulate a legacy database client using callbacks.

    • It exposes a query(sql, callback) method.

    • If the SQL string contains "error", it triggers a failure.

    • Otherwise, it calls the callback with fake data after a short delay.

    • This models a real callback-style API you’d see in older Node.js libraries.

  • Lines 15–17: The DbClientAdapter constructor accepts the legacy client.

...