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-fx

v1.0.2

Published

Simplifies web, file, and parallel tasks with TypeScript async/await

Downloads

8

Readme

async-fx

Simplifies web, file, and parallel tasks with TypeScript async/await

This package makes it easier to perform web requests, access files, call asynchronous functions in parallel using TypeScript and async/await.

This package bundles the following: (each of which is also available individually if desired)

Getting Started

Make sure you're running Node v4 and TypeScript 1.7 or higher...

$ node -v
v4.2.6
$ npm install -g typescript tsd
$ tsc -v
Version 1.7.5

Install the async-fx package...

$ npm install async-fx

Install required typings definitions for the Node.js API...

$ tsd install node

Write some code...

import {File, Parallel, WebRequest} from 'async-fx';
(async function () {
    var urls = ['http://google.com/', 'http://bing.com/', 'http://yahoo.com/'];
    var list = await Parallel.map(urls, async function (url): Promise<string> {
        var response = await WebRequest.get(url);
        return response.content;
    });
    await Parallel.each(list, async function (data) {
        var name = /<title>(.*)<\/title>/.exec(data)[1] + '.html';
        await File.writeTextFile(name, data);
        console.log('file "' + name + '" written');
    });
    console.log('done');
})();

Save the above to a file (index.ts), build and run it!

$ tsc index.ts typings/node/node.d.ts --target es6 --module commonjs
$ node index.js
file "Google.html" written
file "Yahoo.html" written
file "Bing.html" written
done

More Examples

Read a set of three text files, one at a time...

var data1 = await File.readTextFile('data1.txt');
var data2 = await File.readTextFile('data2.txt');
var data3 = await File.readTextFile('data3.txt');

Get the current weather forecast from a JSON feed...

var url = 'http://query.yahooapis.com/v1/public/yql?q=select+item+from+weather.forecast+where+location%3D%2292679%22&format=json';
var data = await WebRequest.json<any>(url);
console.log(data.query.results.channel.item.forecast);

Append a line into an arbitrary series of text files, processing each operation in parallel...

var files = ['data1.log', 'data2.log', 'data3.log'];
await Parallel.each(files, async function (file) {
    await File.writeTextFile(file, '\nPASSED!\n', null, File.OpenFlags.append);
});

Get the top level page content from an arbitrary series of urls, running each request in parallel...

var urls = ['http://google.com/', 'http://bing.com/', 'http://yahoo.com/'];
var result = await Parallel.map(urls, async function (url): Promise<string> {
    var response = await WebRequest.get(url);
    return response.content;
});

Get two pages sleeping for 2 seconds in between...

var page1 = await WebRequest.get('http://www.yahoo.com/news');
await sleep(2000);
var page2 = await WebRequest.get('http://www.yahoo.com/weather');