
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
A synchronous Promise-like response object containing a resolved/rejected state and associated value
A synchronous Promise-like response object containing a resolved/rejected state and associated value
npm install resolution
or
npm install -g resolution
Then import the module into your program:
var Resolution = require('resolution')
A Resolution Object is a response object to inform the caller whether an execution succeeded or failed.
A Resolution Object can also have a return value associated with it.
It can be in one of the following states:
pending - This means the Resolution Object has been initialized, but not yet resolved or rejected
fulfilled - This means the Resolution Object was successful in its operation, or resolved
rejected - This means the Resolution Object was unsuccessful in its operation, or rejected
The state of any Resolution Object can be determined multiple ways. Each Resolution Object has the following properties:
state - String - The name of the current state
pending - Boolean - True if this Resolution Object is in the pending state
fulfilled - Boolean - True if this Resolution Object is in the fulfilled state
rejected - Boolean - True if this Resolution Object is in the rejected state
settled - Boolean - True if the state of this Resolution Object is not pending
locked - Boolean - True if the state and value of this Resolution Object cannot be changed
The associated value can be accessed with the following property:
Here is a quick example:
var myFile = Resolution(function(){
return fs.readFileSync( myFile, 'utf8' );
})
/*
If no error:
myFile.state = 'fulfilled'
myFile.value = <the file>
If there was an error:
myFile.state = 'rejected'
myFile.value = <the Error>
*/
Resolution Objects comes in two types: Immutable and Mutable
Immutable Resolution:
Mutable Resolution:
Along with everything mentioned above, every Resolution Object has the same properties as a Promise does (and more)
Check out the Instance Methods sections of the API for more details
The Resolution Constructor also has the same properties that the Promise Constructor does (and more)
Check out Class Methods section of the API for more details
To see the tests proving that Resolutions and Promises have the same behavior, run the following from within the module directory:
npm run promise-test
or check out tests\promiseTest.js
Just like Promises, a Resolution object is created using an executor function:
Resolution( executor )
executor - Function - Function which will resolve or reject the Resolution. Has the following arguments:
resolve - Function - Function to resolve this Resolution with the given value
reject - Function - Function to reject this Resolution with the given value
Note :
error will be thrown if executor is not a functionExamples
Can be invoked with or without the new keyword
var res = Resolution(function( resolve, reject ){
resolve( 100 );
})
//res.state = 'fulfilled'
//res.value = 100
var res = new Resolution(function( resolve, reject ){
reject( 0 );
})
//res.state = 'rejected'
//res.value = 0
var res = Resolution(function(){})
//res.state = 'pending'
//res.value = undefined
Only the first invocation of resolve or reject will have any effect
var res = Resolution(function( resolve, reject ){
resolve( 100 );
reject( 0 );
})
//res.state = 'fulfilled'
//res.value = 100
Invoking resolve or reject the Resolution asynchronously will not have any effect
var res = Resolution(function( resolve, reject ){
setTimeout(function(){
resolve( 100 );
},1000)
})
//Immediately:
//res.state = 'pending'
//res.value = undefined
// > 1000 ms later:
//res.state = 'pending'
//res.value = undefined
Unlike Promises, a Resolution can also be resolved but returning any value other than undefined
resolve or reject arguments, then the return value will be ignoredvar res = Resolution(function( resolve, reject ){
return 100;
})
//res.state = 'fulfilled'
//res.value = 100
var res = new Resolution(function( resolve, reject ){
reject( 0 );
return 100;
})
//res.state = 'rejected'
//res.value = 0
var res = Resolution(function( resolve, reject ){
return undefined;
})
//res.state = 'pending'
//res.value = undefined
Any errors thrown during the execution of executor will automatically reject the Resolution with the error as the value
error was thrownvar res = Resolution(function( resolve, reject ){
throw new Error()
})
//res.state = 'rejected'
//res.value = Error
var res = Resolution(function( resolve, reject ){
resolve( 100 );
throw new Error()
})
//res.state = 'fulfilled'
//res.value = 100
A Resolution Object can also be initialized into a specific state:
Resolution.resolve( value )
Resolution.reject( value )
Resolution.pending( value )
value - Any - The value associated with this Resolution ObjectExamples
var res = Resolution.resolve(100)
//res.state = 'fulfilled'
//res.value = 100
var res = Resolution.reject(0)
//res.state = 'rejected'
//res.value = 0
var res = Resolution.pending(50)
//res.state = 'pending'
//res.value = 50
As with Promises, a Resolution Object can automatically invoke functions based on the state
then( onResolve, onReject )
onResolve - Function - Function to run if this Resolution Object is resolved.
onReject - Function - Function to run if this Resolution Object is rejected.
For both, the input argument is the value of this Resolution Object
Returns a new Resolution Object resolved to the return value of the callback function.
callback function was not invoked, then the state and value of the new Resolution Object will match this Resolution Objectcallback function throws an error, then the new Resolution Object will be rejected with that error.Examples
var res = Resolution(function( resolve, reject ){
resolve( 100 );
})
.then(function( value ){
console.log('Resolved to ' + value);
},function( value ){
console.log('Rejected to ' + value);
});
//Resolved to 100
var res = Resolution(function( resolve, reject ){
reject( 0 );
})
.then(function( value ){
console.log('Resolved to ' + value);
},function( value ){
console.log('Rejected to ' + value);
});
//Rejected to 0
Because it returns a new Resolution Object, the then's can be chained
var res = Resolution.resolve(100)
.then(function( value ){
return value-1;
})
.then(function( value ){
console.log('Resolved to ' + value);
});
//Resolved to 99
var res = Resolution.resolve(100)
.then(function(){
throw new Error()
})
.then(function( value ){
console.log('Resolved');
},function( value ){
console.log('Error')
});
//Error
catch( onReject )
onReject - Function - Function to run if this Resolution Object is rejected.The input argument is the value of this Resolution Object
Returns a new Resolution Object resolved to the return value of the callback function.
callback function was not invoked, then the state and value of the new Resolution Object will match this Resolution Objectcallback function throws an error, then the new Resolution Object will be rejected with that error.Equivalent to calling then( undefined, onReject )
Examples
var res = Resolution.reject(0)
.catch(function( value ){
console.log('Rejected to ' + value);
});
//Rejected to 0
var res = Resolution.reject(0)
.then(function( value ){
console.log('Resolved to ' + value);
})
.catch(function( value ){
console.log('Rejected to ' + value);
});
//Rejected to 0
var res = Resolution.resolve(100)
.catch(function( value ){
console.log('Rejected to ' + value);
})
.then(function( value ){
console.log('Resolved to ' + value);
});
//Resolved to 100
var res = Resolution.resolve(100)
.then(function( value ){
throw new Error();
})
.catch(function( value ){
console.log('Error');
});
//Error
A Mutable Resolution Object follows the same principals as an Immutable Resolution Object when initializing.
See here for Initializing Immutable Resolutions
The only difference between a Mutable Resolution and Immutable Resolution Objects during initialization is that the state of a Mutable Resolution Object can be set more than once or asynchronously.
var res = Resolution.Mutable(function( resolve, reject ){
resolve(100);
reject(0);
})
//res.state = 'rejected'
//res.value = 0
var res = Resolution.Mutable(function( resolve, reject ){
reject(0);
return 100;
})
//res.state = 'fulfilled'
//res.value = 100
var res = Resolution.Mutable(function( resolve, reject ){
setTimeout(function(){
resolve( 100 );
},1000);
})
//Immediately:
//res.state = 'pending'
//res.value = undefined
// > 1000 ms later:
//res.state = 'fulfilled'
//res.value = 1000
And as with Immutable Resolution Objects, Mutable Resolution Objects can be initialized in a specific state
Resolution.Mutable.resolve( value )
Resolution.Mutable.reject( value )
Resolution.Mutable.pending( value )
value - Any - The value associated with this Resolution ObjectA Mutable Resolution Object also contains then and catch (described here)
Note that then and catch are invoked immediately based on the state of the Resolution Object as that instant.
If the state later changes, these will not be retroactively invoked.
To have callbacks be invoked upon a state change, use the on method (see below)
Mutable Resolution Objects contain the following additional methods:
valuevaluevaluevalueIf no value is provided, then the current value of this Resolution will not change
var res = Resolution.Mutable.resolve( 100 );
//res.state = 'fulfilled'
//res.value = 100
res.reject( 0 );
//res.state = 'rejected'
//res.value = 0
res.reset( 50 );
//res.state = 'pending'
//res.value = 50
res.resolve();
//res.state = 'fulfilled'
//res.value = 50
//res.locked = false
res.lock();
//res.locked = true
//cannot be changed once locked
res.reject( 0 );
//res.state = 'fulfilled'
//res.value = 50
Callbacks can also be connected to the state of the Mutable Resolution Object
* `which` - _String_ - Name of the state change function to connect the callback `fn` to
* Either `'resolve'`, `'reject'`, or `'reset'`
* `fn` - _Function_ - The Callback Function to run whenever the desired state change function is called
* Input argument is the **value** of this **Resolution**
* `once` - _Boolean_ - Whether the `fn` should only be called one time
Examples
var res = Resolution.Mutable.pending( 100 );
res.on('resolve', function( data ){
console.log('Always : ' + data)
})
res.on('resolve', function( data ){
console.log('Once : ' + data)
}, true)
res.resolve(100);
//Always : 100
//Once : 100
res.resolve(50);
//Always : 50
Just like Promises, the Resolution Class has some methods to handle arrays (or other iterable objects) of Resolution Objects
all( iterable )
iterable - iterable - Object to iterate throughReturns a new Resolution Object that is:
iterable object was provided
Erroriterable
iterable
iterableiterable
undefinedExamples
var res = Resolution.all()
//res.state = 'rejected'
//res.value = Error
var res = Resolution.all([])
//res.state = 'fulfilled'
//res.value = []
var res = Resolution.all([Resolution.resolve(100),50])
//res.state = 'fulfilled'
//res.value = [100,50]
var res = Resolution.all([Resolution.resolve(100),Resolution.reject(0)])
//res.state = 'rejected'
//res.value = 0
var res = Resolution.all([Resolution.resolve(100),Resolution.pending(50)])
//res.state = 'pending'
//res.value = undefined
race( iterable )
iterable - iterable - Object to iterate throughReturns a new Resolution Object that is:
Error if no iterable object was providediterable that is not a pending Resolution Object in the iterable is also not a rejected Resolution Object
iterable that is not a pending Resolution Object in the iterable is a rejected Resolution Object
undefined if the iterable is empty or every element in the iterable is a pending Resolution ObjectExamples
var res = Resolution.race()
//res.state = 'rejected'
//res.value = Error
var res = Resolution.race([])
//res.state = 'pending'
//res.value = undefined
var res = Resolution.race([Resolution.pending(50)])
//res.state = 'pending'
//res.value = undefined
var res = Resolution.race([Resolution.resolve(100),50])
//res.state = 'fulfilled'
//res.value = 100
var res = Resolution.race([50,Resolution.resolve(100)])
//res.state = 'fulfilled'
//res.value = 50
var res = Resolution.race([Resolution.resolve(100),Resolution.reject(0)])
//res.state = 'fulfilled'
//res.value = 100
var res = Resolution.race([Resolution.reject(0),Resolution.resolve(100)])
//res.state = 'rejected'
//res.value = 0
var res = Resolution.race([Resolution.pending(50),Resolution.resolve(100)])
//res.state = 'fulfilled'
//res.value = 100
FAQs
A synchronous Promise-like response object containing a resolved/rejected state and associated value
We found that resolution demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.