You can chain asynchronous operations and execute them sequentially
by using Promise.
Of course, not only asynchronous operations but also synchronous operations
can be chained.
Example Code
Promise.resolve()
.then((action, data) -> {
new Thread(() -> {
// Do something
System.out.println("process-1");
action.resolve("result-1");
}).start();
})
.then((action, data) -> {
new Thread(() -> {
// Do something
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("process-2");
action.resolve("result-1");
}).start();
})
.then((action, data) -> {
new Thread(() -> {
// Do something
System.out.println("process-3");
action.resolve("result-1");
}).start();
});
Result in:
process-1
process-2
process-3
Specify the operation to be performed after the promise processing.
In this method,you can specify only "Func" instead of doing "new
Promise(func)".You may omit "new Promise(func)".
Promise.all waits for all fulfillments (or the first rejection).
Promise.all is rejected if any of the elements are rejected.
For example,
if you pass in four promises that resolve after a sleep and one promise
that rejects immediately, then Promise.all will reject immediately.
Since each promise is executed on its own worker thread, the
execution of the promise itself continues on the worker thread.
But, once reject received, Promise.all will move on to then when it receives
reject even if the worker thread is moving
If fulfilled, all results are returned as "List
Promise.all waits for all fulfillments (or the first rejection).
Promise.all is rejected if any of the elements are rejected.
For example,
if you pass in four funcs that resolve after a sleep and one func
that rejects immediately, then Promise.all will reject immediately.
Since each func is executed on its own worker thread, the
execution of the func itself continues on the worker thread.
But, once reject received, Promise.all will move on to then when it receives
reject even if the worker thread is moving
If fulfilled, all results are returned as "List" at
Func#run(Action, List) method.
If rejected, only rejected func results will be returned as "Error" at
Func#run(Action, Error) method.