Facebook Pixel

What is Promise.resolve used for in JavaScript?

Creating an already-fulfilled promise. Use it when you have a value but need a promise (e.g., returning from a function that sometimes does sync work). Example: if (cached) return Promise.resolve(cached); else return fetch(url);

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Creating Promises 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 outer promise adopts the state of the inner promise. If the inner promise fulfills, the outer fulfills with the same value. If the inner rejects, the outer rejects with the same reason. The outer waits for the inner.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.