Can an ES6 JavaScript promise be resolved by anything else if it is not resolved by the executor?

javascript promise w3schools
javascript promise example
async/await javascript
javascript promise then
promise chaining
return new promise
await promise
javascript wait for promise

If a promise is created this way:

myPromise = new Promise(function(resolve, reject) {});

Can this promise be ever resolved by any way? Obviously, the executor is not resolving it in this case, so can the promise be ever resolved?

I think if it is jQuery, it can be resolved by deferred.resolve() so that the related promise deferred.promise() is resolved.

No, the promise can only be resolved or rejected by the resolve and reject functions, respectively.

If these functions aren't called nor exposed, the promise will remain in pending state forever.

That's also a security level, as it helps you ensure the promise you wait for will resolve to the value you expect and when you expect; and allows you to safely return an internal promise, as it is (without any hazards or leaking secrets).

To make a promise, that relies on another promise you don't have control over, but can be resolved (or rejected) manually, regardless of the original, you can use the Promise.race() function:

//The original promise that may pend forever
const original = new Promise(()=>{})

//Controller promise, whose resolver is exposed (or contains the logic needed for "manual" resolution)
let resolve
const controller = new Promise(rs=>{resolve=rs})

//The promise you wait for (and can be resolved or rejected either by `original` or `controller`)
const mixed = Promise.race([original,controller])

mixed.then(console.log)

setTimeout(()=>resolve('Resolved'), 2000)

Promise, A “producing code” that does something and takes time. The analogy isn't terribly accurate, because JavaScript promises are more let promise = new Promise ( function ( resolve , reject ) { // executor (the producing code, "singer") } ) ; A promise that is either resolved or rejected is called “settled”,� The Promise.resolve() method returns a Promise object that is resolved with a given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. This function flattens nested layers of promise-like

Assign the resolve function to a variable outside the scope of the promise

Promise, A Promise is a proxy for a value not necessarily known when the promise is created. You will also hear the term resolved used with promises — this means that That is forced because an ES6 promise chain goes through all the new Promise( // The executor function is called with the ability to resolve� Defer: Promise that can be resolved failed remotely (outside of its body) Delay: Promise that is resolved automatically after a given time; TimeOut: Promise that fails automatically after a given time. Cycle: Re-triggerable promise to manage events with the Promise syntax; Queue: Execution queue based on Promise chaining.

Above solutions are nice, but I think we can create separate function / class / whatever to make it more hermetic

function CreatePromise (executor) {
    let resolve;
    let reject;

    const promise = new Promise((res, rej) => {
        resolve = res;
        reject = rej;
        executor(res, rej)
    })

    return {
        resolve,
        reject,
        promise
    }
}

const { reject, promise } = CreatePromise((resolve) => {
    setTimeout(() => {
        resolve()
    }, 100000)
})

Graceful asynchronous programming with Promises, Promises are a comparatively new feature of the JavaScript language Obviously, that's not an acceptable option; without promises, everything in the The executor will only run when all three promises resolve; when that� First, we create a delay function that returns a promise. The returned promise will resolve after the given number of seconds. Here’s its implementation — const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); In this example, we are using a function to wrap our promise so that it does not execute immediately.

How JavaScript promises actually work from the inside out, What is a Promise? Fulfilled: onFulfilled() will be called (e.g., resolve() was called) will be called (e.g., reject() was called); Pending: not yet fulfilled or rejected According to the definition at Mozilla: It takes an executor function as an so if you try to reject or resolve a promise more than once then it will� Promises inside a loop - Javascript ES6. If you want to learn more about Javascript. check out this articles. Prototypal Inheritance - Javascript Weekly. Understanding Closures in Javascript - Javascript Weekly. Configuring Babel for Node/Express. Before jumping into the code, we will see why we need to write a promise function inside a loop

The Definitive Guide to the JavaScript Promises, Summary: in this tutorial, you will learn about JavaScript promises, what they are, and how to use In JavaScript, a promise is an object that returns a value which you hope to receive in the future, but not now. Inside the executor, you manually call the resolve() function if the executor is completed ES6 Arrow Functions. let promise = new Promise(function(resolve, reject) { // not taking our time to do the job resolve(123); // immediately give the result: 123 }); For instance, this might happen when we start to do a job but then see that everything has already been completed and cached.

ECMAScript 6 promises (2/2): the API, A promise can only be settled once and then stays settled. That usually fulfills the promise (it may not, if you resolve with a promise, as explained later). If an error happened, the executor notifies the promise consumer via reject() . What makes promises so useful for asynchronous functions (with one-off� Understanding JavaScript Promises. In JavaScript, a promise is an object that returns a value which you hope to receive in the future, but not now. Because the value will be returned by the promise in the future, the promise is very well-suited for handling asynchronous operations. It’ll be easier to understand the concept of JavaScript

Comments
  • If you monkeypatched window.Promise, yes, but pretty sure that's not what you're thinking about
  • @CertainPerformance you mean you actually change the mechanics of Promise? For example, when the promise is created, assign the resolve to promise.resolve so that the promise can resolve itself by promise.resolve()?
  • Or just call .resolve() yourself in the constructor, if you really want to muck things up. You're not really changing the mechanics of how Promises work, you're corrupting window.Promise to do something non-spec-compliant
  • See also Why does the Promise constructor need an executor? - ES6 promises don't use the deferred pattern.
  • This Promise constructor isn't doing that, though. If a promise is created this way:
  • But their is nothing to stop you doing it manually inside your promise function
  • What do you mean by "hermetic", and why would that be desirable?
  • When I said hermetic I mean that we do not need to create let resolve; and let reject; each time where we want to do this rejection. Instead I created that CreatePromise which avoid code duplication and hide unnecessary elements, so person who will be using that function do not need to know how we are getting returned values.