Resolveable
Updated on August 22, 2024Source codeTests
Resolveable
is a class that enriches a Promise, allowing it to:
- Asynchronously resolve itself
- Store the response or error
- Store a status (
ready
,resolving
,resolved
, orerrored
)
Construct a Resolveable
instance
The Resolveable
constructor accepts two parameters:
Parameter
Type
Required
Description
getPromise
Function
yes
A function that returns the Promise that will be made resolvable.
options
Object
no
Options for the
Resolveable
instance. See the Resolveable
constructor options section for more guidance.Resolveable
constructor options
Resolveable
's options
object currently does not accept any options.
State and methods
Property
Type
Description
Parameters
Return value
getPromise
Getter/Setter
See return value
N/A
The getPromise
function passed to the constructor.
If you assign a value directly to getPromise
, a setter will pass the new value to setGetPromise
.
status
Getter
See return value
N/A
The status (String) of the promise-resolving process. See the How methods affect status section for more information.
value
Getter
See return value
N/A
The the value resolved from the Promise after calling the
resolve
method.error
Getter
See return value
N/A
The error thrown after unsuccessfully calling the
resolve
method.setGetPromise(newGetPromise)
Function
Sets
getPromise
The new
getPromise
(Array)The
Resolveable
instanceresolve()
Function
Asynchronously resolves the Promise, updating
value
when done.None
The
Resolveable
instanceUsing with TypeScript
The Resolveable
constructor accepts one generic type that you can use to enforce a type for the resolved Promise value. By default, TypeScript will infer the type from the initial state you pass to the constructor, but you can specify the type manually if needed.
const withInferredTypes = new Resolveable(async () => await 0)
withInferredTypes.getPromise = async () => await 'baleada' // Type error
const withManualTypes = new Resolveable<string | number>(async () => await 0)
withManualTypes.getPromise = async () => await 'baleada' // No type error
API design compliance
Spec
Compliance status
Notes
Access functionality by constructing an instance
Constructor accepts two parameters: a piece of state, and an
options
object.The
options
object doesn't currently have any valid properties.Constructor does not access the DOM
Takes the form of a JavaScript Object
State and methods are accessible through properties of the object
Methods always return the instance
Stores the constructor's state in a public getter named after the state's type
getPromise
Has a public method you can use to set a new value for that public getter
setGetPromise
Has a setter for that getter so you can assign a new value directly
Any other public getters that should be set by you in some cases also have setters and
set<Property>
methodsnone
Has at least one additional getter property that you can't (and shouldn't) set directly
status
, response
, error
Has one or more public methods that expose core functionality
resolve
Either has no side effects or has side effects that can be cleaned up with a
stop
methodUses the sentence template to decide what state type should be accepted by a constructor
"A Promise (retrieved by a Promise getter) can be resolved."
Constructor does not accept options that only customize the behavior of public methods, it allows those options to be passed to the method itself as a parameter.
Named after its core action, proper-cased and suffixed with
able