Solution: Bridge Callback and Promise APIs
Convert a callback-based database client into a modern promise-based interface using an adapter.
We'll cover the following...
We'll cover the following...
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
DbClientAdapterconstructor accepts the legacy client.