Table of Contents 📋
# with npmnpm install --save async-status# with yarnyarn add async-status
The goal of this package is to provide a unified and standardized representation of asynchronous status.
It helps to integrate other libraries that translate asynchronous state to synchronous state
(for example Redux).
Instead of using different flags like
hasError, this library represents asynchronous status
as a one-variable - status.
There are 4 possible statuses:
AsyncStatus.IDLE⚫️ - means that nothing happens - it's the initial status
AsyncStatus.PENDING⏳ - means that async action is pending
AsyncStatus.RESOLVED✅ - means that async action has been successfully finished
AsyncStatus.REJECTED❌ - means that an error occurred during async action
There are no constraints about transitions between given statuses.
To give a better understanding of the relationship between
AsyncStatus and asynchronous code let's try with real code:
As you can see, you don't have to introduce any boolean flags like
API reference 📖
The status is a primitive value - string or undefined.
AsyncStatus.IDLE = undefined;AsyncStatus.PENDING = "PENDING";AsyncStatus.RESOLVED = "RESOLVED";AsyncStatus.REJECTED = "REJECTED";
AsyncStatus.all(...statuses: AsyncStatus): AsyncStatus
Combines many statuses into one status using an algorithm similar to
Basically the algorithm is:
- if statuses list is empty, result = IDLE
- if one of the statuses is equal REJECTED, result = REJECTED
- if one of statuses is equal PENDING, result = PENDING
- if all statuses equal RESOLVED, result = RESOLVED
- in other cases, result = IDLE
isAsyncStatus(candidate: any): candidate is AsyncStatus
Checks if a given candidate is an
AsyncStatus (means that it's equal