When you create a promise with new Promise
, you have to tell the constructor when to resolve or reject it in the resolver function at once.
That's inconvenient when we not sure about this. We may need to take out the resolve and reject function.
There are 2 ways to use this feature: public and private.
In this case, the resolve and reject functions are mounted on the promise. Everybody that can access the promise, can resolve or reject it too.
import 'https://better-js.fenz.land/src/promise/make.js';
const promise= Promise.make();
promise.then( ()=> { /*...*/ }, );
promise.catch( ()=> { /*...*/ }, );
promise.finally( ()=> { /*...*/ }, );
async ()=> {
await promise;
}
if( ok )
promise.resolve( value, );
else
promise.reject( reason, );
In this case, the resolve and reject functions are unmounted from the promise. Only the owner can resolve or reject the promise.
const { promise, resolve, reject, }= Promise.make();
promise.then( ()=> { /*...*/ }, );
promise.catch( ()=> { /*...*/ }, );
promise.finally( ()=> { /*...*/ }, );
async ()=> {
await promise;
}
if( ok )
resolve();
else
reject();