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

v0.1.1

Published

Asynchronous coordination primitives for JavaScript and TypeScript

Downloads

9

Readme

async-promise

Asynchronous coordination for JavaScript and TypeScript.

Promise

This contains a polyfill for the native ES6 promise, along with a number of additional coordination primitives to assist in asynchronous application development in JavaScript and TypeScript.

A normal function call in JavaScript is completed synchronously in one of two ways: normal completion that exits the function with a possible return value, or an abrupt completion which results in an exception.

An asynchronous function can return a Promise, which represents the eventual completion of the asynchronous operation in one of two ways: fulfillment of the Promise with a possible return value (an asynchronous 'normal completion'), or rejection of the Promise with a reason (an asynchronous 'abrupt completion').

For example, if you wanted to fetch a remote resource from the browser, you might use the following code to perform a synchronous fetch:

function fetch(url) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, /*async:*/ false);
  xhr.send(null);
  return xhr.responseText;
}

try {
  var res = fetch("...");
  /*do something with res*/
  var value = next(res);
  /*do something with value*/
}
catch(err) {
  /*handle err*/
}

The above example has the unfortunate side effect of blocking the browser's UI thread until the resource is loaded. To be more efficient, we might rewrite this to be asynchronous using Continuation Passing Style:

function fetchCPS(url, callback, errback) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, /*async:*/ true);
  xhr.onload = event => callback(xhr.responseText);
  xhr.onerror = event => errback(xhr.statusText);
  xhr.send(null);
}

fetchCPS("...", 
  res => {
    /*do something with res*/
    nextCPS(res, 
      value => {
        /*do something with value*/
      }, 
      err => {
        /*handle err*/ 
      })},
  err => {
    /*handle err*/
  })

If you need to perform a large number of nested asynchronous calls, Continuation Passing Style can start to look complicated very quickly.

With Promises you would instead write:

import { Promise } from 'async-promise';
function fetchAsync(url) {
  return new Promise((resolve, reject) => {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, /*async:*/ true);
    xhr.onload = event => resolve(xhr.responseText);
    xhr.onerror = event => reject(xhr.statusText);
    xhr.send(null);
  });
}

var resP = fetchAsync("...");
resP.then(res => {
      /*do something with res*/
      return nextAsync(res);
    })
    .then(value => {
      /*do something with value*/
    })
    .catch(err => {
      /*handle err*/
    })

More information can be found in the wiki.