项目作者: paramananda

项目描述 :
A JAVA implementation of Promise .then control flow library.
高级语言: Java
项目地址: git://github.com/paramananda/promise-java.git
创建时间: 2018-01-19T05:54:06Z
项目社区:https://github.com/paramananda/promise-java

开源协议:MIT License

下载


promise-java

A JAVA implementation of Promise .then control flow library for Java and Android.

The Promise object represents the eventual completion (or failure)
of asynchronous method calls, and its resulting value.

A Promise is a proxy for a value not necessarily known when
the promise is created. It allows you to associate handlers
with an asynchronous action’s eventual success value or failure reason.
This lets asynchronous methods return values like synchronous methods:
instead of immediately returning the final value,
the asynchronous method returns a promise to supply the value
at some point in the future.

For more information on Javascript Promise
please visit the official Mozilla Promise documentation

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

This Android Promise Library is slightly different than the native Javascript Promise.
This promise object has two imprtant method i.e. resolve() and reject(),
whenever you are done, your method just call resolve or reject
function based on resultant value.
The resultant value will be automatically passed as argument to the
followng then() or error() function.
It supports above JAVA 1.8

Fork me on Github:
@see Open Github Project

HOW TO USE ?

  • Simply copy Promise.java into your source code folder, now you are ready to play with async flow chain like below.
  1. promiseObject
  2. .then()
  3. .then()
  4. .then() // Do task one after another 'n' number of chain
  5. .error();

How it works ?

Following code snippet demonstrate a complex async call flow into simple code flow.

  1. doSomeTask(int someValue, String extra)
  2. .then(res -> doSecondTask((MyObject) res)) // res is result form doSomeTask()
  3. .then(res -> doThirdTask((OtherObject) res))) // res is result form doThirdTask()
  4. .then(res -> doFourthTask((int) res))) // res is result form doThirdTask()
  5. .then(res -> doFivthTask())
  6. .then(res -> {
  7. // Consume result of the previous function
  8. return true; // done
  9. })
  10. .error(err -> handleError()); // Incase of any p.reject() call from above function error will be available here
  1. public Promise doSomeTask(int someValue, String extra){
  2. Promise p = new Promise();
  3. new Thread(()->{
  4. // do some background operation.
  5. // When your work done, resolve the promise like below;
  6. // After resolve the library will pass the value to doSecondTack()
  7. // Suppose your resultant value is instance of MyObject, then
  8. // When it is error call reject() with some error message or an Exception
  9. // p.reject("Some Error happened")
  10. MyObject myObject = new MyObject();
  11. p.resolve(myObject);
  12. });
  13. return p;
  14. }
  15. public OtherObject doSecondTask(MyObject myInput) {
  16. // Do some syncronous or asyncronous task and return the result,
  17. // Still it work with your promise chain.
  18. // Folloing snipet is just a sample work flow
  19. OtherObject obj = new OtherObject();
  20. return obj;
  21. }
  22. public Promise doThirdTask(OtherObject otherObject){
  23. // Do some task, return value using promise
  24. Promise p = new Promise();
  25. // Your task
  26. p.resolve();
  27. return p;
  28. }
  29. public Promise doFourthTask(){
  30. return doSomePromisableTask(); // I am not writing defination of this fuction, let this function is very similar to
  31. // `doSomeTask()` function
  32. }
  33. public Promise doFivthTask(){
  34. return doSomePromisableTask()
  35. .then(res -> {
  36. // Do some task here
  37. return 1; // this one will be available in the next the or parent then which called this task
  38. });
  39. }

Other Static API of promise :

  1. Promise.all(Promise...).then();
  2. Promise.parallel(Object..., <HandlerFunction>).then();
  3. Promise.series(Object..., <HandlerFunction>).then();

How to use in android?

Download the source file add into your project src.

Promise.java Simply Copy this file into your project

*It need JAVA 1.8 to compile

LICENCE

  1. The MIT License
  2. Copyright (c) 2017-2018 Paramananda Pradhan
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.