Coder Social home page Coder Social logo

async-to-gen's Introduction

async-to-gen

npm Build Status

Turn your JavaScript with async functions into ES6 generators so they can be used in modern browsers or in node.js (v0.12 or newer).

Async functions are an exciting new proposed addition to JavaScript. The v8 team is hard at work getting it right, which means it could appear in future versions of node.js. However if you're impatient like me, then you probably just can't wait to get rid of your promise triangles and callback hell.

You can use Babel to accomplish this, but async-to-gen is a faster, simpler, zero-configuration alternative with minimal dependencies for super-fast npm install and transform time.

Get Started!

Use the command line:

npm install --global async-to-gen
async-to-gen --help
async-to-gen input.js > output.js

Or the JavaScript API:

npm install async-to-gen
var asyncToGen = require('async-to-gen');
var fs = require('fs');

var input = fs.readFileSync('input.js', 'utf8');
var output = asyncToGen(input);
fs.writeFileSync('output.js', output);

Use async-node

Wherever you use node you can substitute async-node and have a super fast async functions aware evaluator or REPL.

$ async-node
> async function answer() {
... return await 42
... }
undefined
> promise = answer()
Promise { <pending> }
> promise.then(console.log)
Promise { <pending> }
42

Use the require hook

Using the require hook allows you to automatically compile files on the fly when requiring in node:

require('async-to-gen/register')
require('./some-module-with-async-functions')

Common Usage

Mocha

Writing tests in mocha? Async functions are super handy for testing any code using promises and already work out of the box! To enable async functions in mocha include the require hook when you run your tests:

mocha --require async-to-gen/register test.js

Then in your tests, use async functions in your it clauses:

describe('My Promising Module', () => {

  it('promises to give a value', async () => {
    expect(await myFunction('input')).to.equal('output')
  })

})

Scripts

Have interactive scripts that require lots of input from the user? Async functions make writing those much easier! Check out interactive-script.

npm install interactive-script

Then write your script:

const interactive = require('interactive-script')
interactive(async (say, ask) => {
  say('What is your favorite number?')
  let num;
  do {
    num = Math.ceil(Math.random() * 100)
  } while (!(await ask(`Is it ${num}? `, 'yN')))
  say(`Great, I think ${num} is a fine number.`)
})

And run it with async-node:

async-node myScript.js

Dead-Simple Transforms

When async-to-gen transforms async functions, it does not affect the location of lines in a file, leading to easier to understand stack traces when debugging.

It also includes a very small (236 byte) conversion function at the bottom of the file.

Before:

async function foo() {
  return await x
}

After:

function foo() {return __async(function*(){
  return yield x
})}

function __async(f){/* small helper function */}

async-to-gen's People

Contributors

chentsulin avatar leebyron avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.