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

cartesian-js

v5.0.0

Published

Async Therefore I am

Downloads

311

Readme

CartesianJS

Async Therefore I Am

A practical functional asynchronous library for JavaScript programmers.

Why Cartesian?

Over the years, I've found myself rewriting utilities for handling Promise-based interactions in every new project I start. I wanted to have a place to pull these functions from and a basic requirement was that they should be modular, composable and purely functional.

This library is meant to give some building blocks form composing your business logic so that you can focus on that rather than needing to getting into the depths of writing a reduce that works asynchronously or figuring out how to batch a certain number of promises at a time.

Installation

NPM:

npm install cartesian-js -S

Yarn:

yarn add cartesian-js --save

The library can be used either by requiring the whole thing or even requiring individual functions. The package is modular and most of the functions are one or two liners, so your build size should not be affected much.

// Load the full build.
const cartesian-js = require('cartesian-js');

// Load the full build with ESM
import * as cartesian-js from 'cartesian-js';

// Pull in a single module
import { compose } from 'cartesian-js';

// Directly load a single module
import compose from 'cartesian-js/compose';

API

batch

batch(Promise, { batchSize?: number; }) => (Array<any>) : Promise<response>

Executes a given number of promises against an array at a time.

Example:

import { batch } from "cartesian-js";

const result = await batch({
    batchSize: 2,
  })(
  (x) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(x);
      }, 10);
    });
  }
)([1, 2, 3, 4, 5]);

console.log(result); // [1, 2, 3, 4, 5]

map

You may provide an optional retry object to the batch options to perform a set number of retries on each result:

import { batch, pipe } from "cartesian-js";

const result = await batch({
    batchSize: 1,
    retry: {
      attempts: 3,
    }
})(
  myAsyncFunction,
)([...arr]);

handle

You may provide an optional handle function to the batch options to catch any errors on an individual item and deal with them without throwing

import { batch, pipe } from "cartesian-js";

const result = await batch({
    batchSize: 2,
    handle: (err) => {
      // Just log the error and continue
      console.warn(err);

      // Whatever is returned here will be part of the resulting array
      // If you don't return anything, it will have an `undefined` hole in the array
      return {
        error: true
      }
    }
})(
  myAsyncFunction
)([...arr]);

compose

compose(...Promises<any>) => (any) : Promise<any>

Compose multiple promises. Composes from right to left.

Example:

import { compose } from "cartesian-js";

const result = await compose(
  (x) => Promise.resolve(`${x}?`), // '8?'
  (x) => Promise.resolve(x + 2), // 8
  (x) => Promise.resolve(x * 2) // 6
)(3);

filter

filter(Promise) => (Array<any>) : Promise<response>

Filters over an array async.

Example:

import { filter } from "cartesian-js";

const result = await filter((x) => {
  return Promise.resolve(x.includes("hi"));
})(["hi", "high", "hay"]);

console.log(result); // ["hi", "hey"]

handle

handle(Promise<any>) => [Error, Response]

Wraps a promise in a catch and returns an array of any caught errors as the first item in the array and the response as the second item.

Example:

import { handle } from "cartesian-js";

const [error, response] = await handle(api.deleteEverything);

if (error) {
  console.log(error);
}

if (response) {
  console.log(response);
}

handleCompose

handleCompose(...Promises<any>) => (any) : Promise<[error, response]>

The same as compose, but wrapped in a handle. Returns an array of [error, response]

Example:

import { handleCompose } from "cartesian-js";

const [error, response] = await handleCompose(
  (x) => Promise.resolve(`${x}?`), // '8?'
  (x) => Promise.resolve(x + 2), // 8
  (x) => Promise.resolve(x * 2) // 6
)(3);

handlePipe

handlePipe(...Promises<any>) => (any) : Promise<[error, response]>

The same as pipe, but wrapped in a handle. Returns an array of [error, response]

Example:

import { handlePipe } from "cartesian-js";

const [error, result] = await handlePipe(
  (x) => Promise.resolve(x * 2), // 6
  (x) => Promise.resolve(x + 2), // 8
  (x) => Promise.resolve(`${x}?`) // '8?'
)(3);

map

map(Promise) => (Array<any>) : Promise<response>

Maps over an array async.

Example:

import { map } from "cartesian-js";

const result = await map((x) => Promise.resolve(x * 3))([1, 2, 3]);

console.log(result); // [3, 6, 9]

pipe

pipe(...Promises<any>) => (any) : Promise<any>

Compose multiple promises. Each will pass its results to the next.

Example:

import { pipe } from "cartesian-js";

const result = await pipe(
  (x) => Promise.resolve(x * 2), // 6
  (x) => Promise.resolve(x + 2), // 8
  (x) => Promise.resolve(`${x}?`) // '8?'
)(3);

reduce

reduce(Promise, ?initialValue) => (Array<any>) : Promise<response>

Reduces over an array async.

Example:

import { reduce } from "cartesian-js";

const [six, seven] = await reduce((prev, x) => {
  return Promise.resolve([...prev, x + 5]);
}, [])([1, 2]);

console.log(six); // 6

retry

({ attempts: number })(fn: () => Promise) => Promise<response>

Waits for a certain number of milliseconds and then proceeds.

Example:

import { retry } from "cartesian-js";

const result = await retry({
  attempts: 3, // Number of times to retry before failing
})(myGreatPromise)(null);

sleep

(milliseconds: number) => (any) : Promise<response>

Waits for a certain number of milliseconds and then proceeds.

Example:

import { sleep } from "cartesian-js";

console.log("Wait a second...");

const result = await sleep({
  timeout: 1000,
})("Okay"); // Sleep for a second

console.log(result); // 'Okay'

timeout

timeout({ timeout: number, errorMessage?: string })(() => Promise<any> => any : Promise<any>

If the timeout happens before a response comes back, we resolve an error.

Example:

import { timeout, handle } from "cartesian-js";

const [error, result] = await handle(
  timeout({
    timeout: 3000,
    errorMessage: "Chronologically challenged",
  })(longRunningPromise)
);

tryCatch

tryCatch(Promise<any>) => [Error, Response] | tryCatch(Fn) => [Error, Response]

Tries to execute a function or a promise and returns either an error or the response.

Example:

import { tryCatch } from "cartesian-js";

const [error, response] = await tryCatch(api.deleteEverything);

if (error) {
  console.log(error);
}

if (response) {
  console.log(response);
}