Gathering detailed insights and metrics for @bugtamer/async-status
Gathering detailed insights and metrics for @bugtamer/async-status
Gathering detailed insights and metrics for @bugtamer/async-status
Gathering detailed insights and metrics for @bugtamer/async-status
npm install @bugtamer/async-status
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
52 Commits
1 Watchers
2 Branches
1 Contributors
Updated on May 30, 2023
Latest Version
1.0.3
Package Id
@bugtamer/async-status@1.0.3
Unpacked Size
20.31 kB
Size
6.41 kB
File Count
7
NPM Version
6.14.14
Node Version
14.17.4
Published on
Jan 15, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Manages the status of an async process.
npm i @bugtamer/async-status
1const bugtamer = require("@bugtamer/async-status") 2const dataAsyncStatus = new bugtamer.AsyncStatus();
or
1const bugtamer = require("@bugtamer/async-status/lib/async-status") 2const dataAsyncStatus = new bugtamer.AsyncStatus();
1import { AsyncStatus } from '@bugtamer/async-status/lib/async-status'; 2const dataAsyncStatus = new AsyncStatus();
1dataAsyncStatus.start(); 2try { 3 data = await fetchData(); 4 dataAsyncStatus.end(); 5} catch (error) { 6 dataAsyncStatus.abort(); 7}
1dataAsyncStatus.start(); 2const subscription = fetchData().subscribe( 3 response => { 4 data = response; 5 dataAsyncStatus.end(); 6 }, 7 error => { 8 dataAsyncStatus.abort(); 9 } 10);
Current State | Method called / Sentence | Outcome |
---|---|---|
new AsyncStatus() | idle state | |
idle | start() | ongoing state |
ongoing | end() | idle state |
ongoing | abort() | idle state |
ongoing | start() | Throw an error |
idle | end() | Throw an error |
idle | abort() | Throw an error |
Do not try to manage these errors, just fix your code. They point out that some method should never have called.
Sentence | Description |
---|---|
dataAsyncStatus.attempts | returns the number of calls to start() |
dataAsyncStatus.successfulAttempts | returns the number of calls to end() |
dataAsyncStatus.failedAttempts | returns the number of calls to abort() |
dataAsyncStatus.resetAttemptStats() | all previous counters are set to 0 |
In this section we understand by call a call to any of the following methods: start()
, end()
or abort()
.
There is no process activity.
dataAsyncStatus.isIdle | Returns |
---|---|
When start() was never executed or the last call was end() or abort() | true |
In any other case | false |
There is a process in progress.
dataAsyncStatus.isOngoing | Returns |
---|---|
When the last call was start() and therefore neither end() nor abort() have been called yet | true |
In any other case | false |
In this section we understand by call a call to any of the following methods: start()
, end()
or abort()
.
dataAsyncStatus.wasSuccessful | Returns |
---|---|
When end() was the last method called | true |
In any other case | false |
dataAsyncStatus.wasFailed | Returns |
---|---|
When abort() was the last method called | true |
In any other case | false |
In milliseconds (ms):
dataAsyncStatus.elapsedTime | Returns |
---|---|
when start() was never called | AsyncStatus.UNDEFINED_TIME (-1 ) |
when start() was called but end() or abort() has not yet been called | Time elapsed since the call to start() to current time |
when start() was called and eventually end() or abort() was also called | Elapsed time from call to start() to end() or abort() call |
Using a single instance of AsyncStatus
to control multiple independent asynchronous processes that overlap in time could lead to erratic behavior in your program.
start()
throws an error when is called more than Number.MAX_SAFE_INTEGER
times (although is nearly unreachable).
1// const bugtamer = require("@bugtamer/async-status/lib/async-status") 2const bugtamer = require("@bugtamer/async-status") 3 4 5function showStats(asyncStatus, message) { 6 console.log(message) 7 console.log(` - Attempts: ${asyncStatus.attempts}`) 8 console.log(` - successful: ${asyncStatus.successfulAttempts}`) 9 console.log(` - failed: ${asyncStatus.failedAttempts}`) 10 console.log(` - State:`) 11 console.log(` - idle: ${asyncStatus.isIdle}`) 12 console.log(` - ongoing: ${asyncStatus.isOngoing}`) 13 console.log(` - Outcome:`) 14 console.log(` - successful: ${asyncStatus.wasSuccessful}`) 15 console.log(` - failed: ${asyncStatus.wasFailed}`) 16 console.log(` - Time elapsed: ${asyncStatus.elapsedTime} ms`) 17} 18 19 20// Let's show where the Internation Space Station currently is. 21console.log("Let's see where the ISS is with Node " + process.version); 22 23// We can use any package from NPM since they are all built in. 24var getJSON = require("async-get-json"); 25 26 27const status = new bugtamer.AsyncStatus(); 28showStats(status, 'new AsyncStatus()') 29 30status.start() 31showStats(status, 'start()') 32 33 34const url = "http://api.open-notify.org/iss-now.json"; // change it to make it fail 35try { 36 // And we can use ES7 async/await to pull the ISS's position from the open API. 37 var result = await getJSON(url); 38 39 status.end() 40 showStats(status, 'end()') 41} catch (error) { 42 status.abort() 43 showStats(status, 'abort()') 44} 45 46 47if (!!result) { 48 // RunKit will automatically display the last statement and try to find its best representation: 49 result.iss_position; 50}
1Let's see where the ISS is with Node v14.20.1 2new AsyncStatus() 3 - Attempts: 0 4 - successful: 0 5 - failed: 0 6 - State: 7 - idle: true 8 - ongoing: false 9 - Outcome: 10 - successful: false 11 - failed: false 12 - Time elapsed: -1 ms 13start() 14 - Attempts: 1 15 - successful: 0 16 - failed: 0 17 - State: 18 - idle: false 19 - ongoing: true 20 - Outcome: 21 - successful: false 22 - failed: false 23 - Time elapsed: 1 ms 24end() 25 - Attempts: 1 26 - successful: 1 27 - failed: 0 28 - State: 29 - idle: true 30 - ongoing: false 31 - Outcome: 32 - successful: true 33 - failed: false 34 - Time elapsed: 75 ms
No vulnerabilities found.
No security vulnerabilities found.