npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bugtamer/async-status

v1.0.3

Published

Asynchronous process status helper.

Downloads

8

Readme

AsyncStatus JS

Manages the status of an async process.

Table Of Content

Installation

As project dependency

npm i @bugtamer/async-status

Available at npmjs.com

As script dependency

'require' demo example

const bugtamer = require("@bugtamer/async-status")
const dataAsyncStatus = new bugtamer.AsyncStatus();

or

const bugtamer = require("@bugtamer/async-status/lib/async-status")
const dataAsyncStatus = new bugtamer.AsyncStatus();

'import' demo example

import { AsyncStatus } from '@bugtamer/async-status/lib/async-status';
const dataAsyncStatus = new AsyncStatus();

Basic usage snippets

async / await

dataAsyncStatus.start();
try {
    data = await fetchData();
    dataAsyncStatus.end();
} catch (error) {
    dataAsyncStatus.abort();
}

Observable

dataAsyncStatus.start();
const subscription = fetchData().subscribe(
    response => {
        data = response;
        dataAsyncStatus.end();
    },
    error => {
        dataAsyncStatus.abort();
    }
);

Status management / Class interface

Use

| 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.

Check attempt stats

| 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 |

Check current state

In this section we understand by call a call to any of the following methods: start(), end() or abort().

Idle State

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 |

Ongoing state

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 |

Check last outcome state

In this section we understand by call a call to any of the following methods: start(), end() or abort().

A successful outcome

| dataAsyncStatus.wasSuccessful | Returns | |-----------------------------------------|---------| | When end() was the last method called | true | | In any other case | false |

A failed outcome

| dataAsyncStatus.wasFailed | Returns | |-------------------------------------------|---------| | When abort() was the last method called | true | | In any other case | false |

Measure the time

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 |

Final notes

  • 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).

Examples

// const bugtamer = require("@bugtamer/async-status/lib/async-status")
const bugtamer = require("@bugtamer/async-status")


function showStats(asyncStatus, message) {
    console.log(message)
    console.log(`  - Attempts:     ${asyncStatus.attempts}`)
    console.log(`    - successful: ${asyncStatus.successfulAttempts}`)
    console.log(`    - failed:     ${asyncStatus.failedAttempts}`)
    console.log(`  - State:`)
    console.log(`    - idle:       ${asyncStatus.isIdle}`)
    console.log(`    - ongoing:    ${asyncStatus.isOngoing}`)
    console.log(`  - Outcome:`)
    console.log(`    - successful: ${asyncStatus.wasSuccessful}`)
    console.log(`    - failed:     ${asyncStatus.wasFailed}`)
    console.log(`  - Time elapsed: ${asyncStatus.elapsedTime} ms`)
}


// Let's show where the Internation Space Station currently is.
console.log("Let's see where the ISS is with Node " + process.version);

// We can use any package from NPM since they are all built in.
var getJSON = require("async-get-json"); 


const status = new bugtamer.AsyncStatus();
showStats(status, 'new AsyncStatus()')

status.start()
showStats(status, 'start()')


const url = "http://api.open-notify.org/iss-now.json"; // change it to make it fail
try {
    // And we can use ES7 async/await to pull the ISS's position from the open API.
    var result = await getJSON(url);

    status.end()
    showStats(status, 'end()')
} catch (error) {
    status.abort()
    showStats(status, 'abort()')
}


if (!!result) {
    // RunKit will automatically display the last statement and try to find its best representation:
    result.iss_position;
}

Example output

Let's see where the ISS is with Node v14.20.1
new AsyncStatus()
  - Attempts:     0
    - successful: 0
    - failed:     0
  - State:
    - idle:       true
    - ongoing:    false
  - Outcome:
    - successful: false
    - failed:     false
  - Time elapsed: -1 ms
start()
  - Attempts:     1
    - successful: 0
    - failed:     0
  - State:
    - idle:       false
    - ongoing:    true
  - Outcome:
    - successful: false
    - failed:     false
  - Time elapsed: 1 ms
end()
  - Attempts:     1
    - successful: 1
    - failed:     0
  - State:
    - idle:       true
    - ongoing:    false
  - Outcome:
    - successful: true
    - failed:     false
  - Time elapsed: 75 ms