Socket
Socket
Sign inDemoInstall

asynchrony

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    asynchrony

Auto unwrap promise parameters for clean asynchronous code


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
40.4 kB
Created
Weekly downloads
 

Readme

Source

asynchrony

Asynchrony is a tiny function that unwraps Promises passed as arguments to functions. Doing so saves you from "then hell", which is like callback hell, but slightly hotter. This project is not a joke. Really.

Examples below to see how you can use asynchrony.

Installation

npm install asynchrony

Comparison with Promises Code

Code with asynchrony. See how functions are wrapped in asynchrony.

var asynchrony = require('asynchrony');

//Get tweets for a handle
var getTweetsFor = asynchrony(function(name) {
    return new Promise(function(resolve, reject) {
        if (name === "jeswin") {
            setTimeout(function() {
                resolve(["Tweet1:t.co/666", "Tweet2:t.co/667"]);
            }, 500);
        } else {
            reject("Unknown user");
        }
    });
});

//Find urls in a tweet
var parseTweetsForUrls = asynchrony(function(tweets) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            var urls = tweets.map(function(tweet) {
                return tweet.split(":")[1];
            });
            resolve(urls);
        }, 100);
    });
});

//Expand a url using twitters api
var expandUrlUsingTwitterApi = asynchrony(function(url) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve("example.com/" + url);
        }, 100);
    });
});

//fetch via http
var httpGet = asynchrony(function(url) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve("Lorem ipsum dolor....");
        }, 100);
    });
});

//Look ma, not many then()s!
var tweets = getTweetsFor("jeswin");
var shortUrls = parseTweetsForUrls(tweets);
var mostRecentShortUrl = asynchrony(function(shortUrls) {
    var mostRecentShortUrl = shortUrls[0];
    return expandUrlUsingTwitterApi(mostRecentShortUrl);
})(shortUrls);

var responseBody = httpGet(mostRecentShortUrl);
responseBody.then(function(resp) {
    console.log(resp);
});

Code without asynchrony.

/*
    Example taken from https://blog.domenic.me/youre-missing-the-point-of-promises/
    Implementation of getTweetsFor(), parseTweetsForUrls() etc removed for brevity
*/

getTweetsFor("domenic")
    .then(function (tweets) {
        var shortUrls = parseTweetsForUrls(tweets);
        var mostRecentShortUrl = shortUrls[0];
        return expandUrlUsingTwitterApi(mostRecentShortUrl); // promise-returning function
    })
    .then(httpGet) // promise-returning function
    .then(
        function (responseBody) {
            console.log("Most recent link text:", responseBody);
        },
        function (error) {
            console.error("Error with the twitterverse:", error);
        }
    );

Keywords

FAQs

Last updated on 15 Jan 2015

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