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

unsync

v1.0.3

Published

Write asynchronous code in ES6 without all the mess.

Downloads

6

Readme

Unsync

Unsync is a JavaScript library that simplifies asynchronous promises by using generators.

Requirements

To use Unsync, you need a JavaScript engine that supports both promises and generators:

  • Node.js v4.0 or above
  • Google Chrome 39 or above
  • Firefox 31 or above
  • Opera 26 or above
  • Android 5.1 or above
  • Microsoft Edge 13 or above

If you need backwards compatability, you can use babel.js + core-js.

API

unsync(generator, [thisArg, ...bindArgs])

Creates a new unsynced function based on the generator function generator.

thisArg and bindArgs behave as if passed to Function.prototype.bind.

Basics

You create an unsynced function by calling unsync:

var myFunc = unsync(function*(x, y, z) {
  // function body
});

The unsynced function is called like a normal function, and returns a promise, which is fulfilled when the function returns.

myFunc()
  .then(function() {
    // the function has returned
  });

Calling asynchronous functions

You can call any asynchronous function that returns a promise using the yield operator:

unsync(function*() {
  // calling asynchronous function
  yield asyncFunc();

  // this will not be reached until asyncFunc() has finished
});

If the promise resolves to a value, this value will be returned by the yield expression:

unsync(function*() {
  // calling asynchronous function with result
  var result = yield asyncFuncWithResult();

  // result will be the value resolved by the promise of asyncFuncWithResult()
});

You can call multiple asynchronous functions in parallel by using the yield operator on an array:

unsync(function*() {
  // calling multiple asynchronous functions
  var results = yield [ fooFunc(), barFunc(), bazFunc() ];

  // this will not be reached until all functions have finished,
  // `results' will be an array with the results from each function
});

Returning values and catching errors

If the unsynced function has a return value, the promise will resolve to this value:

var myFunc = unsync(function*() {
  return 1234;
});

myFunc()
  .then(function(result) {
    // result = 1234;
  });

If an uncaught error occurs in the unsynced function, or any of the called functions, then the promise is rejected.

var myFunc = unsync(function*() {
  yield funcWithError();
});

myFunc()
  .catch(function(reason) {
    // this will be called when an error occurs,
    // `reason' will be the reason an error occurred
  });

Using a custom Promise implementation

If you want to use a custom Promise implementation, such as Q or Bluebird, you can simply change the unsync.Promise property:

// Q
unsync.Promise = Q.Promise;

// Bluebird
unsync.Promise = Bluebird.Promise;

unsync.defer()

The defer method is implemented for compatability with asynchronous functions that do not return promises.

When called, the defer method returns an object with the following properties:

  • promise: A deferred promise.
  • resolve([value]): A function that resolves the promise, with the value value.
  • reject([reason]): A function that rejects the promise, with the reason reason.
  • callback(err, [value]): A Node-style callback, that resolves or reject the promise based on err.

Example

var waitAndPrint = unsync(function*(message) {
  // sleep for 3 seconds
  var deferred = unsync.defer();
  setTimeout(deferred.resolve, 3000);
  yield deferred.promise;

  // print message
  console.log(message);
});

waitAndPrint('Hello, World!');