Code Monkey home page Code Monkey logo

asynccbn's Introduction

asynccbn

Babel Plugin to transpile EcmaScript7 async function transpiled into callbacks.

Build Dependencies Coverage Docs

NPM

still working, carefull in production enviroments

Why

Promises and generators need more memory and are slower then callbacks.

install

npm install asynccbn

usage

Use it as a babeljs plugin

sample 1: defining an async function

input:

  async function divide(a,b)
  {                         
    return a/b;
  }            

output:

  function divide(a, b, callback) {
    callback(null, a / b);
  }
  

sample 2: invoking async function

input:

  async function fn() { 
    return await divide(8,2) + await divide(10,2);
  }

output:

 divide(8, 2, function(err$, res$1) {
   if (err$) return callback(err$);
   divide(10, 2, function(err$, res$2) {
     if (err$) return callback(err$);
     callback(null, res$1+res$2);
   });
 });  

sample 3: callback hell

input:

async function fn() { 
  var x=10;                   
  var a=await divide(2,2);    
  var y=10;                   
  var b=await divide(4,2);    
  var c=await divide(6,2);    
  var d=await divide(16,2)/2; 
  var e;                      
  e=await divide(10,2);       
  return x+a+b+c+d+e-y;       
}

output:

function fn(callback) {
  var x=10;
  divide(2, 2, function(err$, a) {
    if (err$) return callback(err$);
    var y=10;
    divide(4, 2, function(err$, b) {
      if (err$) return callback(err$);
      divide(6, 2, function(err$, c) {
        if (err$) return callback(err$);
        divide(16, 2, function(err$, res$4) {
          if (err$) return callback(err$);
          var d=res$4/2;
          var e;
          divide(10, 2, function(err$, res$5) {
             if (err$) return callback(err$);
             e=res$5;
             callback(null, x+a+b+c+d+e-y);
          });
        });
      });
    });
  });
}

asynccbn's People

Contributors

teintinu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.