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

async-deferrer

v1.0.4

Published

function that returns a promise or resolves it

Downloads

50

Readme

asyncDeferrer building status

Creates a deferrer, which is a function that behaves very similar to a deferred object: it allows the get and the resolve of a promise.

It allows the creation of more expressive tests. The reason for this library is just matter of expressiveness.

Quick Use

Install with npm:

npm install async-deferrer

Use in your code:

import makeAsyncDeferrer from 'async-deferrer'

test("setTimeout logs correctly", async () => {
  const log = []
  const timeoutFinished = makeAsyncDeferrer()
 
  setTimeout(async () => {
    log.push("timeout")
    timeoutFinished(true)
  })
 
  log.push("begin")
  await timeoutFinished()
  log.push("end")
 
  expect(log).toEqual(["begin", "timeout", "end"])
})

makeAsyncDeferrer()

It creates a function that can be used as a defer.

import makeAsyncDeferrer from 'async-deferrer'

const asyncDeferrer = makeAsyncDeferrer()

asyncDeferrer(resolutionValue?: any): Promise

The resulting function returns always a promise that will be satisfied when the function is called with an argument.

import makeAsyncDeferrer from 'async-deferrer'

const asyncDeferrer = makeAsyncDeferrer()

// you can get the promise at any moment as follows
const promise = asyncDeferrer()

// it resolves the promise with the given value
asyncDeferrer('resolve promise with this string')

// you can await the promise as follows
await promise
// or directly using a function call
await asyncDeferrer()

The promise is resolved once, further invokes with other arguments are ignored.

You can call to the asyncDeferrer function as many times as you want.

asyncDeferrer.reject(rejectionValue): Promise

You can reject the promise if you consider convenient:

asyncDeferrer.reject('promise rejected')

try {
  await asyncDeferrer()
} catch (error) {
  // logs 'promise rejected'
  console.log(error)
}

Because the promise is resolved once, further invokes with other arguments are ignored. Further invokes to resolve the promise are also ignored.

Alternatives

There are other libreries or ways that could help you to writte the desired asynchronous code.

(pDefer)[https://github.com/sindresorhus/p-defer]

It is a library from (Sindre Sorhus)[https://github.com/sindresorhus) part of his large list of p-*.

The code for the same presentation shouwn before could be something like:

import pDefer from 'p-defer'

test("setTimeout logs correctly", async () => {
  const log = []
  const timeoutDeferred = pDefer()
 
  setTimeout(async () => {
    log.push("timeout")
    timeoutDeferred.resolve()
  })
 
  log.push("begin")
  await timeoutDeferred.promise
  log.push("end")
 
  expect(log).toEqual(["begin", "timeout", "end"])
})

Plain promises

The same behaviour can be achieved with plain promises.

test("setTimeout logs correctly", async () => {
  const log = []
 
  const timeoutPromise = new Promise((resolve) => {
    setTimeout(async () => {
      log.push("timeout")
      resolve()
    })
  })
 
  log.push("begin")
  await timeoutPromise
  log.push("end")
 
  expect(log).toEqual(["begin", "timeout", "end"])
})

Why asyncDeferrer instead of Promises or defer

It is cleaner an easier to read.

If we compare all three presented cases:

// asyncDeferrer
timeoutFinished(true)
// pDefer
timeoutDeferred.resolve()
// Promise
resolve()

The first of three writings is the most easy to enderstand version. Just reading we knew that we are telling that the timeout is finished. The promise version could be rewrote as:

// rewrote of Promise version
const timeoutFinished = new Promise((flagTimeoutFinished) => {
  setTimeout(async () => {
    log.push("timeout")
    flagTimeoutFinished()
  })
})

But it still has the new Promise(() => {}) boilerplate code and not always easy to use.

Using asyncDeferrer as checkpoints

It in fact is the intention of this library, to ensure that the execution flow is what we expect.

import makeAsyncDeferrer from "async-deferrer"
 
test('synchronize multiple steps in the correct order', async () => {
    const log = []
    const startStep1 = makeAsyncDeferrer()
    const startStep2 = makeAsyncDeferrer()
    const finishedStep2 = makeAsyncDeferrer()
    ;(async () => {
      await startStep2()
      log.push('step 2')
      finishedStep2(true)
    })()
    ;(async () => {
      await startStep1()
      log.push('step 1')
      startStep2(true)
    })()

    log.push('begin')
    startStep1(true)
    await finishedStep2()
    log.push('end')

    expect(log).toEqual(['begin', 'step 1', 'step 2', 'end'])
  })

See also

  • async-barrier: it is the library that inspired this one, and originally was designed to ensure that multiple async functions reaches the same point at the same time

  • pDefer: traditional defer library generated from the system Promise

  • p-*: lots of Promise utilities created by the guy behind pDefer.