0.0.0 • Published 10 years ago

sweet-es7-async v0.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

sweet-es7-promise

Build Status

A sweet.js macro for ES7 async functions.

You're probably better off using the Traceur Compiler, but this is a fun experiment.

Usage

var getComments = async function (postId) {
    var response = await fetch("/posts/" + postId);

    if ( response.headers["X-Has-No-Comments"] ) {
        return;
    }

    var post = await response.body.to("json");
    return post.comments;
};

Translates to:

var getComments = function (postId) {
    return Promise.resolve().then(function () {
        return fetch("/posts/" + postId).then(function (response) {
            if ( response.headers["X-Has-No-Comments"] ) {
                return;
            }

            return response.body.to("json").then(function (post) {
                return post.comments;
            });
        });
    });
};

Missing features

  • try-catch blocks with await.