Git Product home page Git Product logo

future-js's Introduction

future-js

A JavaScript homage to the Dartlang Completer API.

Using it is simple. You create an asynchronous function of some sort. This function creates a Completer which you use to track your progress, and eventually complete the operation. From this funciton, return a future.

var MyObj = function() {
    var _completer;
    
    // The asynchronous action performed
    this.startAsync = function(arg1, arg2) {
        _completer = new future.Completer();
        
        return _completer.future();
    };
    
    // Complete the action (perhaps as a callback)
    this.endAsync = function() {
        _completer.complete("SUCCESS", "CODE 9");
    };
};

Code wishing to monitor the progress of the function can then use a really slick API to monitor your progress.

var my_obj = new MyObj();

function handle_completion() {
    say("This is so awesome that we're done!");
}

function do_other_stuff(msg, code) {
    say("More incredible things happened, '" + msg + "'");
}

// Note we're saving the future for later use
var future =
    my_obj.startAsync().then(handle_completion).then(do_other_stuff);

// Completing the asynchronous funtion calls all our handlers
my_obj.endAsync();

You're handlers will be called with the same arguments as to complete.

You can even add handlers after your work is done as long as you kept a reference to the future.

future.then(function() {
    say("We're calling this handler immediately since the operation is done.");
});

There's lots more you can do with this, including handling exceptions. For more examples check out the /spec/future-spec.js or index.html.

future-js's People

Contributors

markbennett avatar

Stargazers

Yasuhiro SHIMIZU avatar

Watchers

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