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

sync-defer

v1.3.2

Published

A Node.js module for synchronous waiting execution proxy based on `LRUCache` and `Promise` to handle asynchronous processing across multiple services.

Downloads

13

Readme

SyncDefer build-status npm license

English | 简体中文

A Node.js module for synchronous waiting execution proxy based on LRUCache and Promise to handle asynchronous processing across multiple services.

Why?

Scenario: We have a service A that needs to make a synchronous request to service B. Service B needs to process this request by sending the task to service C. This task also needs to go through the processing of services D and E, and finally, service E will send the processed task result to service B. Once service B receives the processing result, it returns it to service A.

The problem that arises is: After service E submits the result to service B, how can service B return the result to service A?

Before solving the problem, let's break down the steps.

We divide this process into two steps, namely sync and defer. We mark the steps waiting for task processing as defer and the steps of sending task results as sync, as shown in the diagram:

sync-defer

The sync-defer module abstracts and encapsulates these two steps and make them independent.

The core design uses LRUCache to cache task IDs, allowing matching between sending requests and receiving results based on ID. It uses Promise to wait for processing and resolves or rejects the received results, thereby achieving synchronous waiting execution.

Getting Started

Install Dependencies

npm i sync-defer

Dependency Import

// for commonjs
const { SyncDefer } = require('sync-defer')
// for esm
import { SyncDefer } from 'sync-defer'

new SyncDefer(options)

const syncDefer = new SyncDefer({
  ttl: 500, // Cache expiration time in milliseconds, default is 5 minutes
  max: 10 // Maximum cache count, default is 500
})

defer()

Wait for the result returned by the sync method.

const result = await syncDefer.defer('id')

sync()

Synchronize normal results:

syncDefer.sync('id', { result: true })

Synchronize exceptional results:

syncDefer.sync('id', null, new Error())

Deferred

// for commonjs
const { Deferred } = require('sync-defer')
// for esm
import { Deferred } from 'sync-defer'

const deferred = new Deferred()

deferred.resolve({ bool: true })
deferred.reject(new Error())
const res = await deferred.promise

You can check the test code for a complete usage example.

License

Released under the MIT License.