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

dyna-sync

v1.2.2

Published

Syncronize calls and promises

Downloads

13

Readme

dyna-sync - Synchronize async function calls and promises

syncFunctions

Synchronizes async methods

TS type: syncFunctions = (...functions: Array<((next?: any) => void) | undefined | null>): void

Usage

As arguments, pass the functions with 1st arg a callback to proceed the next.

The callback argument is optional.

Example

import {syncFunctions} from "dyna-sync";

syncFunctions(

  next => setTimeout(() => {
    output.push('First');
    next();
  }, 100),

  next => setTimeout(() => {
    output.push('Second');
    next(undefined);
  }, 500),

  () => output.push('Third'),
  () => undefined,

  () => {
    expect(output.join()).toBe('First,Second,Third');
    testDone();
  },
);

Use in React

The syncFunctions is ideal for squential React's setState() calls.

In react, you cannot call the setState() on next the other because the last will override the changes of the previous since the setState() is async!.

You cannot do this!

Let's say that at this point this.state.firstName==='' and this.state.lastName===''

  this.setState({firstName: 'John'});
  this.setState({lastname: 'Smith'});  

What we have now on state is this.state.firstName==='' and this.state.lastName==='Smith'

The firstName is still an empty string, since the 2nd call overrides the first sync call.

You can solve this like this with syncFunctions

syncFunctions(
  next => this.setState({firstName: 'John'}, next);
  next => this.setState({lastname: 'Smith'}, next);
);  

What we have now on state is this.state.firstName==='John' and this.state.lastName==='Smith'.

This happens because setState's 2nd argument of React is a callback called when the state is updated.

The 2nd function of the syncFunctions is called only when next of the 1st function is called.

syncPromises

Synchronizes async Promises

The syncPromises is working like the Promise.all, it returns an array with the resolved items but is calls the promises synchronously.

TS type: syncPromises = <TResolve = any, >(...promisedFunctions_: Array<(() => Promise<TResolve>)>): Promise<TResolve[]>

Usage

Example with calls

import {syncPromises} from "dyna-sync";

syncPromises(
  () => fetch('http://api.example.com/metadata').then(updateMetadata),
  () => fetch('http://api.example.com/users').then(updateUsers),
)

The updateUsers if the 2nd promise is called only when the 1st promise is resolved.

Example with output and typesript

import {syncPromises} from "dyna-sync";

syncPromises<IUsers>(
  () => fetch('http://api.example.com/users/section/support'),  // Suppose that returns array of users
  () => fetch('http://api.example.com/users/section/billing'),  // Suppose that returns array of users
)
    .then((resultUsers: Users[][]) => [].concat(resultUsers))

When the above syncPromises is resolved, it will return an array of two items since we have two promises. It is precisely like the Promise.all with the difference that one promise is executed each time in a row.

If each of the above promises returns an array of users, then we have an array of two arrays of users.

syncPromisesResilience

The syncPromisesResilience is working like the syncPromises but it doesn't break on errors.

TS type:

type PromiseResult<TData> =
  | { data: TData }
  | { error: Error };
syncPromisesResilience = 
  async <TData, >(promisedFunctions: Array<() => Promise<TData>>): 
    Promise<Array<PromiseResult<TData>>>

As argument, it gets an array (instead of arguments array) and it returns array of two types of objects:

  • The {data: TData} or
  • the {error: Error}

Usage

const results = await syncPromisesResilience<{ name: string }>(
  [
    () => fetch({customerId: '8745924'}),
    () => fetch({customerId: '4859623'}),
    () => fetch({customerId: '6927464'}),
    () => fetch({customerId: '0385633'}),
    () => fetch({customerId: '5893244'}),
  ]
);

Output example

  [
    Object {
      "data": Object {
        "name": "Joihn Smith",
      },
    },
    Object {
      "data": Object {
        "name": "Nora Jones",
      },
    },
    Object {
      "data": Object {
        "name": "Christofer Lee",
      },
    },
    Object {
      "error": [Error: General load error], // Here we have an error property, instead of data
    },
    Object {
      "data": Object {
        "name": "Monica Williams",
      },
    },
  ]