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

v0.1.7

Published

Extend native Promise in es6 with additional helper methods

Downloads

3

Readme

Extend native Promise in es6 with additional methods

Example

import 'promise-addition';

const fs = Promise.promisify(require('fs'));
fs.readFile(__filename, 'utf8').then(content => console.log(content));

Promise.fromCallback(function (callback) {
  callback(null, result); // or callback(err);
}).then((result) => {});

Promise.sleep(10).then(() => console.log('10ms past'));

Promise.each([1, 2, 3], (val, index) => val + 1).then(result => console.log(result));// [2, 3, 4]

// run 3 promises concurrently all the time
Promise.throttle([1, 2, 3, 4], 3, (val, index) => val + 1).then(result => console.log(result));// [2, 3, 4, 5]

db.query().timeout(10000); // if query not completed in 10 seconds, reject as timeout

db.query().delay(10000); // if query completed less than 10 seconds, wait then go next

db.query()
  .finally(() => db.disconnect()) // use finally() to release resources
  .then(users => console.log(users))
  .catch(err => console.log(`query error: ${err.message}`));

API

Promise.sleep(ms: int)

Promise version of setTimeout()

Promise.promisify(input: function|object)

Promisify node callback-style function or the object who has these functions.

Promise.fromCallback(func: (cb)=>any)

func is async function that called cb at the end, either cb(null, result) or cb(err)

Promise.each(input: arr|promise, callback: (item, index)=>promise)

input can be an array or a promise that returns array. Run each item in the array with the callback one by one, as opposed to Promise.all(). If someone in the callback rejects/throws, promise will be rejected immediately and the remains will not be run. This function is same as Promise.throttle(input, 1, callback)

Promise.throttle(input: arr|promise, concurrentNum: int, callback: (item, index)=>promise)

Run the specified number items with the callback concurrently. If one resolved, then run next one. Reject immediately if some one failed.

promise.timeout(ms: int, message?: string)

If the promise is fulfilled less than the specified time(ms), no timeout occurs. Otherwise it will be rejected with the timeout message(default timeout);

promise.delay(ms: int)

If the promise is fulfilled less than the specified time(ms), wait.

promise.finally(callback: ()=>any)

If the promise fulfilled, run the finally callback first, then run .then() or .catch(). If the callback returns a rejected promise or throws an error, it will trigger the global unhandledRejection event(process.on('unhandledRejection', cb)). Others return will be ignored.

License

Licensed under MIT

Copyright (c) 2017 Tian Jian