GithubHelp home page GithubHelp logo

asyncawait's Introduction

Async Await

Using the AsyncAwait pattern, you can write any asynchronous function with a callback as last argument in a synchronous way.

Example

var multiply = function (x, y, callback) {
  callback(x * y);
}

Now, we want to do 1 * 2 * 3 * 4 using an asynchronous multiply function:

function main() {
  multiply(1, 2, function (res) {
    multiply(res, 3, function (res) {
      multiply(res, 4, function (res) {
        console.log(res);
      })
    })
  })
}
main() // 24

We just wrote spaghetti code. Now let's look the same code with async, await and yield:

var main = async(function () {
  var [res] = yield await(multiply)(1, 2);
  [res] = yield await(multiply)(res, 3);
  [res] = yield await(multiply)(res, 4);
  console.log(res);
});
main() // 24

Using async and await, we can easily combine asynchronous functions to write a new one:

var factorial = async(function (n, callback) {
  var res = 1;
  for (; n > 0; --n) {
    [res] = yield await(multiply)(res, n);
  }
  callback(res);
});

We can either use it the old way:

var main = function () {
  factorial(4, function (res) {
    console.log(res);
  })
}
main() // 24

or the new way:

var main = async(function () {
  var [res] = yield await(factorial)(4);
  console.log(res);
});
main() // 24

Similar Projects

spawn(function*() {
    var data = yield $.ajax(url);
    $('#result').html(data);
    var status = $('#status').html('Download complete.');
    yield status.fadeIn().promise();
    yield sleep(2000);
    status.fadeOut();
});
var res1, res2;
await {
    doOneThing(defer(res1));
    andAnother(defer(res2));
}
thenDoSomethingWith(res1, res2);
private static async Task<int> Sum2ValuesAsyncWithAssistance() { 
    Task<int> task1 = Task.Factory.StartNew(() => 1); 
    Task<int> task2 = Task.Factory.StartNew(() => 2); 

    int value1 = await task1; 
    int value2 = await task2; 

    return value1 + value2; 
}
  • Kaffeine: Code Rewrite, Doesn't work with loops.
get = {
  err, data = jQuery.get! "/"
  if !err, process data
}

asyncawait's People

Contributors

vjeux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.