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-to-sync

v1.0.7

Published

async-to-sync is help you for Javascript asynchronous function to synchronous function.

Downloads

8

Readme

async-to-sync

NPM npm npm
async-to-sync is help you for Javascript asynchronous function to synchronous function.
You don't should know async/await, also Promise!!

Why did you make async-to-sync

Finally, async/await added ES2017(isn't publish yet).
async/await is help you for Javascript asynchronous function to synchronous function.
But, it isn't use to easy, also you have to know Promise in ES2015.
So, I made it easy to use async/await & Promise for asynchronous function to synchronous function.

Getting Started

Installation

npm

npm i -S async-to-sync

yarn

yarn add async-to-sync

Usage

If you didn't know browser or node of supported status, you would visit below link.
ECMAScript 6 compatibility table | Promise
Node.js ES2015/ES6 | Promise
ECMAScript 2016+ compatibility table | async
Node.js ES2017 support | async
Attention!
async-to-sync doesn't contains babel-polyfill,
So if you want to it, you should install it.
npm

npm i -S babel-polyfill

yarn

yarn add babel-polyfill

or use cdn.
If you want to learn quickly how to use, you should read examples.

How to import async-to-sync

In webpack
In async/await & Promise support browser
import ats from 'async-to-sync';
In async/await & Promise or ES2015 not support browser

You must use babel-polyfill.
You must import babel-polyfill before async-to-sync.
And you don't like to transpile ES2015+ to ES5 using babel, dont't use ES2015 Syntax. (for IE)

import 'babel-polyfill';
import ats from 'async-to-sync/module/no-es2017';
In browser (CDN isn't support yet, just wait a minute!)
In async/await & Promise support browser
<script src="node_modules/async-to-sync/browser/es2017/index.min.js"></script>
In async/await & Promise or ES2015 not support browser

You must use babel-polyfill.
You must import babel-polyfill before async-to-sync.
And you don't like to transpile ES2015+ to ES5 using babel, dont't use ES2015 Syntax. (for IE)

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.23.0/polyfill.min.js"></script>
<script src="node_modules/async-to-sync/browser/no-es2017/index.min.js"></script>
In Node.js
In async/await & Promise support Node

If you want to use import syntax, please use transpiler like babel.

const ats = require('async-to-sync');
In async/await & Promise or ES2015 not support Node

You must use babel-polyfill.
You must import babel-polyfill before async-to-sync.
And if you want to use import syntax, please use transpiler like babel.

require('babel-polyfill');
const ats = require('async-to-sync/module/no-es2017');

How to use

You have some asynchronous function,

  • setTimeout(or setInterval)
var a = function() {
  setTimeout(function() {
    console.log(123);
  }, 2000);
};

var b = function(b) {
  setTimeout(function() {
    console.log(b);
  }, 1000);
};
var fallback = function(e) {
  alert('Error: ' + e);
};

var xhr = function(url, method) {
  method = method || 'get';
  return new Promise(function(res, rej) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);
    xhr.responseType = "json";
    xhr.onreadystatechange = function() {
      if(xhr.readyState === 4) { // 4 means request is done.
        if(xhr.status === 200) { // 200 means status is successful
          res(xhr.response);
        } else {
          rej(xhr.status);
        }
      }
    };
    xhr.send();
  });
};

var _fetch = function(url, method, headers, body) {
  method = method || 'get';
  headers = headers || null;
  body = body || null;
  return fetch(url, {
    method, headers, body
  }).then(function(res) {
    return res.json({});
  });
};

var c = function(url) {
  xhr(url).then(function(data) {
    console.log(data);
  }).catch(function(e) {
    fallback(e)
  });
};

var d = function(url) {
  _fetch(url).then(function(data) {
    console.log(data);
  }).catch(function(e) {
    fallback(e)
  });
};

Attention!
You must follow some rules.

  • Add callback function parameter to your asynchronous function.
  • Execute callback function in your asynchronous function last.
// You must add cb(callback) parameter
var a = function(cb) {
  setTimeout(function() {
    console.log(123);
    // You must execute callback function.
    cb();
  }, 2000);
};

// You must add cb(callback) parameter last
var b = function(b, cb) {
  setTimeout(function() {
    console.log(b);
    cb();
  }, 1000);
};

var c = function(url, cb) {
  xhr(url).then(function(data) {
    console.log(data);
    cb();
  }).catch(function(e) {
        fallback(e)
  });
};

var d = function(url, cb) {
  _fetch(url).then(function(data) {
    console.log(data);
    cb();
  }).catch(function(e) {
    fallback(e)
  });
};
Real usage
ats(Array arrAsync[, Function fallback])
  1. arrAsync's Type is Array.
    It contains asynchronous functions.
    They execute synchronous.
  2. fallback's Type is Function, it is optional.
    It execute when an error occurs, then rest functions are stop.
var arrUrl = [
  'https://perfectacle.github.io/mock/test.json',
  'https://perfectacle.github.io/mock/test2.json'
];

var arrAsync = [
  a,
  // If You want to pass arguments or bind this, You could use bind method.
  c.bind(null, arrUrl[0]),
  // You also can use ES2015 arrow function for pass arguments,
  // but you must also pass callback function parameter too.
  cb => b(2, cb),
  a,
  cb => d(arrUrl[1], cb),
  b.bind(null, 33)
];

ats(arrAsync, fallback);

Support Platform

Chrome | Firefox | IE| Node.js| --- | --- | --- | --- | Latest ✔ | Latest ✔ | 9+ ✔ | 6+ ✔ |