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

forest-promises

v1.0.1

Published

Forest Promises: create and track dependency forests of promises

Downloads

5

Readme

logo

forest-promises

Forest Promises: create and track dependency forests of promises.

Documentation

Forest

Comming soon.

Tree

Constructor

let tree = new Tree(promise, name = 'task', track = 0, ident = 0)

A Tree wraps a Promise. It has a state variable that tracks its wrapped promise state.

// Initial state is 'pending'.
this.state = STATES.PENDING

// Wraps the promise and tracks its state.
this.promise = promise.then(value => {
  this.state = STATES.FULFILLED
  return value
}, reason => {
  this.state = STATES.REJECTED
  throw reason
})

It also has a name and a ident, mainly used to display a tree's status string. The track paramenter enables, if greater than zero, a log of the tree's status string every track milliseconds.

Static methods

Tree.STATES is a getter to a static constant enumerate object that contains all possible states of a Tree instance: pending, fulfilled and rejected.

Instance methods

The Tree class wraps its promise instance methods then, catch and finally. It also provides a status_str getter that serializes the tree instance to a idented human friendly string that displays its state and name. The resolved method returns wheter the tree's promise is resolved.

The log method writes the tree's status string to the issued logger. The track method writes it until the tree state is resolved.

Examples

The following code showcase the stand alone usage of Trees.

import Tree from 'forest-promises/tree.mjs'

let toggle = false

let executor = (resolve, reject) => {
  setTimeout(() => {
    if (!toggle)
      resolve(toggle = !toggle)
    else 
      reject(toggle = !toggle)
  }, 100)
}

new Tree(new Promise(executor), '1. fulfilled', 50)

new Tree(new Promise(executor), '2. rejected', 50)
  .catch(reason => console.log(` i  2. rejected --> a tree was rejected.`))

new Tree(new Promise(executor), '3. not tracked')

let tree = new Tree(new Promise(executor), '4. rejected', 50)
tree.catch(reason => {
    console.log(` i  the tree "${tree.name}" was rejected.`)
  })

The executor function is used to create promises. It alternates between resolve and reject calls, so that odd trees will be fulfilled and even trees will be rejected. Trees 1, 2 and 4 are tracked every 50 milliseconds. Tree number 3 is not tracked.

Output
 ◆  1. fulfilled
 ◆  2. rejected
 ◆  4. rejected
 i  2. regected --> a tree was rejected.
 i  the tree "4. rejected" was rejected.
 ✔  1. fulfilled
 ✘  2. rejected
 ✘  4. rejected

Credits

Image credit. Literally inspired the creation of this module.