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

promise-with-events

v1.0.1

Published

Simple lib to use promises with events

Downloads

5

Readme

GitHub Workflow Status (with event) minified size npm

Compatibility

check here

Bundle Analyzer

Quick Menu

How to use

Install

npm i promise-with-events

#or

yarn add promise-with-events

#or

pnpm add promise-with-events

Simple example

import { createWatchEvent, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

createWatchEvent(promiseExample1, {
  eventName: "key1",
  autoStart: true,
})

onResolveEvents((error, resolve1) => {
  console.log(resolve1) //output = "example1"
}, ["key1"])

Documentation

Watch Events

createWatchEvent receive 2 params:

  1. promises: (() => promises)[]
  2. key: that is the event name OR config:
type TConfig = {
  eventName: string
  autoStart?: boolean
  promiseMethod?: "all" | "allSettled" | "any" | "race"
}
...
createWatchEvent(promise1, "key1")
...
createWatchEvent(promise1, {
  eventName: "key1",
  autoStart: true,
  promiseMethod: 'race',
})
...
createWatchEvent([promise1, promise2], "key2")
...
createWatchEvent([promise1, promise2], {
  eventName: "key2",
  autoStart: true,
  promiseMethod: 'race',
})

Single Promise

import { createWatchEvent, startEvents, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

createWatchEvent(promiseExample1, "key1")

startEvents(["key1"])

onResolveEvents((error, resolve1) => {
  console.log(resolve1) //output = "example1"
}, ["key1"])

Multi Promises

import { createWatchEvent, startEvents, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

const promiseExample2 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example2");
  });
};

createWatchEvent([promiseExample1, promiseExample2], "key1")

startEvents(["key1"])

onResolveEvents((error, resolve1) => {
  console.log(resolve1) //output = ["example1", "example2"]
}, ["key1"])

Multi Events

import { createWatchEvent, startEvents, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

const promiseExample2 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example2");
  });
};

const promiseExample3 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example3");
  });
};

const promiseExample4 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example4");
  });
};

createWatchEvent([promiseExample1, promiseExample2], "key1")

createWatchEvent([promiseExample3, promiseExample4], "key2")

startEvents(["key1", "key2"])

onResolveEvents((error, resolve1, resolve2) => {
  console.log(resolve1) //output = ["example1", "example2"]
  console.log(resolve2) //output = ["example3", "example4"]
}, ["key1", "key2"])

Auto Start

Note: startEvents doesn't works with autoStart active Possible values: true | false (default value = true)

import { createWatchEvent, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

const promiseExample2 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example2");
  });
};

createWatchEvent([promiseExample1, promiseExample2], {
  eventName: "key1",
  autoStart: true,
})

onResolveEvents((error, resolve1) => {
  console.log(resolve1) //output = ["example1", "example2"]
}, ["key1"])

Promise Method

Note: promiseMethod doesn't works with single promise Possible values: all | allSettled | any | race (default value = all)

import { createWatchEvent, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

const promiseExample2 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example2");
  });
};

createWatchEvent([promiseExample1, promiseExample2], {
  eventName: "key1",
  autoStart: true,
  promiseMethod: "any",
})

onResolveEvents((error, resolve1) => {
  console.log(resolve1) //output = "example1"
}, ["key1"])

Start Events

Note: Doesn't works with autoStart: true

import { startEvents } from "promise-with-events";

startEvents(["key1"])

startEvents(["key1", "key2"])

Events

All events receive 2 params:

  1. callback: returns error, ...responses (response for each key)
  2. keys: string[]

Note: error on callback its about code errors

onStartEvents

Calls callback param when all promises are resolved

Note: Doesn't works with autoStart: true

...
onStartEvents((error) => {
  console.log('ok')
}, ["key1"])
...
onStartEvents((error) => {
  console.log('ok')
}, ["key1", "key2"])

onResolveEvents

Calls callback param when all promises are resolved

...
onResolveEvents((error, resolve1) => {
  console.log(resolve1)
}, ["key1"])
...
onResolveEvents((error, resolve1, resolve2) => {
  console.log(resolve1)
  console.log(resolve2)
}, ["key1", "key2"])

onRejectEvents

Calls callback param when all promises are rejected

...
onRejectEvents((error, reject1) => {
  console.log(reject1)
}, ["key1"])
...
onResolveEvents((error, reject1, reject2) => {
  console.log(reject1)
  console.log(reject2)
}, ["key1", "key2"])

onFinallyEvents

Calls callback param when all promises are finished

...
onFinallyEvents((error, finally1) => {
  console.log(finally1.response) //could be undefined, according promise result
  console.log(finally1.error) //could be undefined, according promise result
}, ["key1"])
...
onFinallyEvents((error, finally1, finally2) => {
  console.log(finally1.response)
  console.log(finally1.error)
  console.log(finally2.response)
  console.log(finally2.rerror)
}, ["key1", "key2"])

Advanced

Reuse Watched Events

import { createWatchEvent, onResolveEvents } from "promise-with-events";

const promiseExample1 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example1");
  });
};

const promiseExample2 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000, "example2");
  });
};

const promiseExample3 = () => {
  return new Promise((resolve) => {
    setTimeout(resolve, 1000, "example3");
  });
};

const watchedPromise1 = createWatchEvent([promiseExample1, promiseExample2], {
  eventName: "key1",
  autoStart: true,
  promiseMethod: "any",
});

const watchedPromise2 = createWatchEvent([watchedPromise1, promiseExample3], {
  eventName: "key2",
  autoStart: true,
});

createWatchEvent([watchedPromise1, watchedPromise2], {
  eventName: "key3",
  autoStart: true,
  promiseMethod: "all",
});

onResolveEvents((error, resolve1) => {
  console.log(resolve1); //output = "example1"
}, ["key1"]);

onResolveEvents((error, resolve2) => {
  console.log(resolve2); //output = ["example1", "example3"]
}, ["key2"]);

onResolveEvents((error, resolve3) => {
  console.log(resolve3); //output = ["example1", ["example1", "example3"]]
}, ["key3"]);

How to contribute

To contribute, make sure to follow the steps bellow:

  1. Create a new branch:

     git checkout -b feat/your-new-feature
  2. Make your changes, add unit tests (with jest) and test with npm link

    On promise-with-events project:

     npm link

    On your app/project:

     npm link promise-with-events

    This will create a symlink into your node_modules app, and you can test iteratively. You can check more about npm-link here

  3. Before to push your changes to origin, open your pull request and fill all required fields.

    1. Make sure to fill the Release section with what your pull request changes. This section is required to merge pull request.
  4. Set a required semver label according to your change:

    1. semver:patch: used when you submit a fix to a bug, enhance performance, etc;
    2. semver:minor: used when you submit a new component, new feature, etc;
    3. semver:major: used when you submit some breaking change, etc;
    4. semver:prerelease: used when you submit a prerelease (ex: 1.0.0-beta.1);
    5. semver:bypass: used to update docs, or something that doesn’t affect the build.

Info: Once you have merged your pull request, with all required fields, GitHub Actions will be responsible to create a new build and publish on stage environment.

License

MIT License