According to MDN documentation for Promise,
The
Promise()constructor createsPromiseobjects. It is primarily used to wrap callback-based APIs that do not already support promises.
With that in mind:
Promise and returning it?Promise constructor function?Promise constructor, you have to register a callback somewhere right? I mean you can't avoid having a callback that at-least resolves the promise? In other words, we have to wrap it like this somewhere in the library, correct?Promise.resolve(theValue). If you want to wait for multiple promises you'd use Promise.all(allThePromises). And of course every call to .then returns a new promise..then. Having said all that, ES2017 introduced async functions which are basically syntactic sugar for promises. async functions always return a promise and you can use await to unwrap promises. Example:
async function get() {
return await Promise.resolve(42);
}
get().then(console.log);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With