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

fmait

v0.0.2

Published

An asynchronous, concurrent library for creating efficient, easy-to-understand promise pipelines.

Downloads

2

Readme

Fmait

Fmait is a function that allows easy transformations over arrays, asynchronously. It provides one additional level of abstraction from async/await, and makes writing, clean, concurrent code a piece of cake.

Installation

To install:

npm install fmait

To import:

const fmait = require("fmait");

Or, if you are using ES6 modules:

import fmait from "fmait";

Use

Let's use a very real situation as an example. You want to create a function that takes a list of Github users, and fins the first follower of each of those users. It wouldn't be hard to write such a program with async/await, or even promise chaining. However, fmait makes writing such a program possible without littering your code with temporary variables and messy Promise.all calls. Let's start by creating the initial function:

function getFirstFollowers(users){

}

In order to use async/await, which fmait uses internally and needs to be used externally, make all your functions that use fmait async.

async function getFirstFollowers(users){

}

fmait is an async function, so we can return the result of calling it with await:

async function getFirstFollowers(users){
    return await fmait(/*The magic happens here*/);
}

fmait takes a array of callbacks, used as transformations, as its first parameter, which we'll go over later. For the second parameter, fmait takes an array that the transformations from the first parameter will be applied to:

async function getFirstFollowers(users){
    return await fmait([/*transformation callbacks go here*/], users);
}

First, the list of users need to be transformed into API urls. Github has a wonderful API that we can use:

async function getFirstFollowers(users){
    return await fmait([
        x => "https://api.github.com/users/" + x
    ], users);
}

As one can see, this looks a bit like a .map call. We have a callback function that is mapped over the array. However, behind the scenes, fmait converts the return value of that callback function to a promise using Promise.resolve, which it then awaits.

You may be worried that fmait will individually await each item of the array. Good news: It dosen't! It uses Promise.all to await all of the items of the array concurrently. Now, we need to fetch the data from the url. You may be tempted to create an async callback function, but your callback function needs to return a Promise:

async function getFirstFollowers(users){
    return await fmait([
        x => "https://api.github.com/users/" + x,
        x => fetch(x), // The result of the last callback is passed to the next. This creates a pipeline of functions
        x => x.json() // The promise is resolved behind the scenes with await, allowing you to continue to the next transformation as if you had awaited the promise manually
    ], users);
}

So, the general layout of the fmait function is (ignoring error handling):

Step 1:

Map the first transformation in the list of callbacks over the array. If the callback dosen't return a promise, make it return one with Promise.resolve.

Step 2:

Make the entire array of promises concurrent in execution with Promise.all, await that, and replace the old array with the array of results.

Step 3:

If all the transformations have been applied, return from the function. Otherwise, using the next transformation in the list of callbacks, repeat.

So, to complete the function:

async function getFirstFollowers(users){
    return await fmait([
        x => "https://api.github.com/users/" + x,
        x => fetch(x),
        x => x.json(),
        x => fetch(x.followers_url),
        x => x.json(),
        x => x[0],
        x => x.login
    ], users);
}

And that's how to use fmait.

Enjoy!