Socket
Socket
Sign inDemoInstall

async-fx

Package Overview
Dependencies
50
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-fx

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


Version published
Weekly downloads
6
increased by500%
Maintainers
1
Install size
3.78 MB
Created
Weekly downloads
 

Readme

Source

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');

Keywords

FAQs

Last updated on 10 Mar 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc