Return new Promise((resolve, reject) => { callbackApi(args, (err, result) => { if (err) reject(err); else resolve(result); }); }). The callback follows the error-first convention.
How do you wrap a callback-based API in a promise in JavaScript?
More FAQs in How to Create a Promise in JavaScript
Use new Promise((resolve, reject) => { ... }). The executor runs immediately. Call resolve(value) to fulfill or reject(reason) to reject. For already-settled promises, use Promise.resolve(value) or Promise.reject(reason).
Immediately and synchronously, when the promise is created. The executor is not deferred. But resolve/reject can be called asynchronously (inside a setTimeout, event handler, etc.).
The promise is rejected with the thrown error. The Promise constructor catches thrown errors automatically. You do not need to try/catch inside the executor.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.