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

args-promise

v1.3.0

Published

Use Promise with multiple arguments.

Downloads

8

Readme

中文README

Installation

npm

npm i -S args-promise

yarn

yarn add args-promise

CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

Or you can import the ES Modules compatible build like this:

<script type="module">
  import ArgsPromise from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/ArgsPromise.esm.min.js'
</script>

Usage

ArgsPromise behaves like native Promise.

const ArgsPromise = require('args-promise').default

new ArgsPromise((resolve, reject) => {
    resolve('foo', 'bar')
}).then((a, b) => {
    console.log(a, b)  // --> 'foo' 'bar'
})

new ArgsPromise((resolve, reject) => {
    reject('foo', 'bar')
}).then((a, b) => {
    console.log(a, b)  // not executed
}).catch((a, b) => {
    console.log('catched', a, b)  // --> 'catched' 'foo' 'bar'
})

If you want to pass multiple arguments to the handle function of next then(), you can return an Array like this:

new ArgsPromise(resolve => {
    resolve()
}).then(() => {
    return ['foo', 'bar']
}).then((a, b) => {
    console.log(a, b)  // --> 'foo' 'bar'
})

await and the methods of Promise (such as Promise.allSettled()) is also effective for ArgsPromise.

let p1 = new ArgsPromise(resolve => {
    setTimeout(() => {
        resolve()
    }, 1000)
})
let p2 = new ArgsPromise(resolve => {
    setTimeout(() => {
        resolve()
    }, 3000)
})
Promise.allSettled([p1, p2])
    .then(() => {
        console.log('allSettled')  // print 'allSettled' after 3s
    })
async function foo() {
    await new ArgsPromise((resolve) => {
        setTimeout(() => {
            resolve()
        }, 3000)
    })
    console.log('bar')
}
foo()  // print 'bar' after 3s

But there is a problem that you can only get the first argument by using Promise.allSettled() and await directly like this:

let p = new ArgsPromise(resolve => {
  resolve(1, 2, 3, 4, 5)
})
Promise.allSettled([p]).then(result => {
  console.log(result[0].value)  // --> 1
})

let args = await new ArgsPromise(resolve => {
  resolve(1, 2, 3, 4, 5)
})
console.log(args)  // --> 1

To get all the arguments, you can use .pack() like this:

let p = new ArgsPromise(resolve => {
  resolve(1, 2, 3, 4, 5)
}).pack()
Promise.allSettled([p]).then(result => {
  console.log(result[0].value)  // --> [ 1, 2, 3, 4, 5 ]
})

let args = await new ArgsPromise(resolve => {
	resolve(1, 2, 3, 4, 5)
}).pack()
console.log(args)  // --> [ 1, 2, 3, 4, 5 ]

Referring to await-to-jsArgsPormise provides .to() to help you to handle simple errors like this:

let p1 = new ArgsPromise((resolve, reject) => {
	resolve('hello', 'world')
})
let [err1, values1] = await p1.to()
if(err1) {
	console.log(err1)
} else {
	console.log(values1)  // --> [ 'hello', 'world' ]
}


let p2 = new ArgsPromise((resolve, reject) => {
	reject('err')
})
let [err2, values2] = await p2.to()
if(err2) {
	console.log(err2)  // --> [ 'err' ]
} else {
	console.log(values2)
}

The executor function which passed into ArgsPromise's constructor receives the third parameter. It is a function that allows you to set some resident variables. In the same Promise Chain, resident variables will always be passed into the handle function of .then(), .catch() and .finally().

There is an example of setting and receiving resident variables:

new ArgsPromise((resolve, reject, resident) => {
    resident('resident-0', 'resident-1')
    resolve(1, 2, 'hello')
}).then((...args) => {
    console.log(...args)    // --> 1 2 'hello' 'resident-0' 'resident-1'
    return ['foo', 'bar']
}).then((...args) => {
    console.log(...args)    // --> 'foo' 'bar' 'resident-0' 'resident-1'
    return 123
}).then((...args) => {
    console.log(...args)    // --> 123 'resident-0' 'resident-1'
}).then((...args) => {
    console.log(...args)    // --> 'resident-0' 'resident-1'
})