Code Monkey home page Code Monkey logo

async.js's Introduction

async.js -- async function wrapper

Note

This project is mainly for demo/learning/testing. Normally you will not need to use it in production, because ES7 async/await has been available in Babel and other transpilers. I recommend you to just use them. And eventually JavaScript engines in the browsers and node.js/io.js will also support async/await natively.

The only exception is if you can not use transpilers for any reason or your transpilers has not supported async/await feature, you could just use it!

There are also many other libraries which provide the same functionality such as co. The advantage of this project is, it's very simple, only 30 lines code :)

Introduction

async convert a generator to an async function, which returns a promise.

Sample

var asyncSum = async(function *sum(a, b) {
	var valueA = yield a
	var valueB = yield b
	return valueA + valueB
})

var asyncResult = asyncSum(Promise.resolve(1), waitAndReturn(1000, 2))

asyncResult.then(function (value) {
	console.assert(value === 3)
})

function waitAndReturn(ms, value) {
	return new Promise(function (resolve) {
		setTimeout(function () { resolve(value) }, ms)
	})
}

TODO

  • Add commented version

async.js's People

Contributors

hax avatar

Watchers

James Cloos avatar galenye 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.