Giter Club home page Giter Club logo

java-promise's Introduction

Java Promise

There are many IO's operation without dependency.If we make them asynchronous, The IO's throughput would be imporved greatly. As we know, asynchronous programming will make the code hard to read. Promise is the specification to resolve it. However, Java doesn't provided it to us. This project, Java promise is promise implements by Java1.7 exatcly.

The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.

Syntax

    new Promise(new Resolver() {
        public void execute(OnFulfill<Object, Object> onFulfill, OnReject<Object, Object> onReject) throws Exception {
            ...
        }
    } )

Parameters

  1. Resolver is the same as: function(resolve, reject){...}

Description

A Promise is in one of these states:

  • pending:initial state, not fulfilled or rejected.
  • fulfilled:meaning that the operation completed successfully.
  • rejected:meaning that the operation failed.

Method

static method

  1. Promise.all(Promise... promises) Promise.all(Promise.resolve(1), Promise.resolve(2))
  2. Promise.race(Promise... promises) Promise.race(Promise.resolve(1), Promise.reject(2))
  3. Promise.resolve(value) Promise.resolve("resovle")
  4. Promise.reject(value) Promise.reject("reject")

instance method

  1. promise.then(OnFulfill onFulfill, OnReject onReject)
  2. promise.then(OnFulfill onFulfill)
  3. promise.Catch(OnReject onReject)

Example1

	//if the random index is equal to 0 , print "success result : 1",
	//the random index is equal to 1, print "error result : 0"
     new Promise(new Resolver() {
            public void execute(OnFulfill<Object, Object> onFulfill, OnReject<Object, Object> onReject) throws Exception {
                int index = new Random().nextInt(2);
                if (index == 0) onFulfill.execute(index);
                else onReject.execute(index);
            }
        }).then(new OnFulfill<String, Integer>() {
            public String execute(Integer i) {
                return "success result : " + i;
            }
        }, new OnReject() {
            public Object execute(Object args) {
                return "error result : " + args;
            }
        })

Example2

	//print "last result : 1"
	Promise.resolve("1").then(new OnFulfill() {
            public Object execute(Object args) {
                System.out.println("last result : " + args);
                return null;
            }
        });
	
	//print "This is my exception, last result is 1"
	Promise.resolve("1").then(new OnFulfill() {
            public Object execute(Object args) throws Exception{
                throw new Exception("This is my exception, last result is "+args);
            }
        }).Catch(new OnReject<Object, Exception>() {
            public Object execute(Exception e) throws Exception {
                System.out.println(e.getMessage());
                return null;
            }
        });

About Promise :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://www.promisejs.org/

java-promise's People

Contributors

hackingwu avatar kagawagao avatar

Watchers

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.