https://github.com/chpengzh/jpromise
simple async task utils for android(Java/Kotlin) dev
https://github.com/chpengzh/jpromise
Last synced: 4 months ago
JSON representation
simple async task utils for android(Java/Kotlin) dev
- Host: GitHub
- URL: https://github.com/chpengzh/jpromise
- Owner: chpengzh
- Created: 2017-01-21T03:52:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-21T04:46:19.000Z (over 8 years ago)
- Last Synced: 2024-12-27T06:41:48.397Z (5 months ago)
- Language: Java
- Size: 85.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jPromise
`jPromise` is a simple util for async task programming in android develop. Just enjoy async task program in JS way!
## ~~Bad Format~~
```java
Lg.e("第一个任务开始了");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Lg.e("第一个任务结束了");
Lg.e("第二个任务开始了, data = " + 1);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Lg.e("第二个任务结束了");
Lg.e("第三个任务开始了, data = " + "second");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Lg.e("第三个任务结束了, 异步任务完成");
}
}, 2000);
}
}, 2000);
}
}, 2000);
```## Do it in promise way
```java
final String clientId = UUID.randomUUID().toString();
Promise.start("Some Group Name", new Starter() {
@Override
public void onStart(@NonNull final Return start) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
start.next(1001);
}
}, 1_000);}
}).then("some step description here", new Task() {
@Override
public void onTask(Integer data, @NonNull final Return ret) {
Log.d("MainActivity", "client id is " + clientId);
Log.d("MainActivity", "data from last step is " + data);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ret.next(null);
}
}, 1_000);}
}).then(new Task() {
@Override
public void onTask(@Nullable String nullable, @NonNull Return ret) {
Log.d("DemoActivity", "data from last step is " + nullable);}
}).execute();
```## Promise for Kotlin (KPromise)
```kotlin
KPromise.start("Some Group Name") { start ->
Handler().postDelayed({
start.next(100)
}, 1000L)}.then("some step description here") { int, kReturn ->
Log.d("DemoActivity", "data from last step is $int")
Handler().postDelayed({
kReturn.next("some string")
}, 1000L)}.then { str, kReturn ->
Log.d("DemoActivity", "data from last step is $str")}.execute()
```