# Maintaining order of execution in Async method calls

**URL:** <https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [January 17, 2022, 10:09am UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766 "2022-01-17T10:09:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![prachi](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@prachi](https://community.temporal.io/u/prachi)\
**Post date:** [January 17, 2022, 10:09am UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/1 "2022-01-17T10:09:28Z")

</div>

Hi Team,

I have this piece of code to execute two methods asynchronously so that I can receive signals meanwhile, but I also want to maintain the order these methods are executed.

```auto
List<Promise<Void>> acts = new ArrayList<>();
acts.add(Async.procedure(activities::uploadSignature, id, signature));
acts.add(Async.procedure(activities::submitSignature, id));

private CompletablePromise<Void> uploaded = Workflow.newPromise();
uploaded.completeFrom(Promise.allOf(acts));

```

Is there a way I can ensure uploadSignature is always called before submitSignature, while maintaining the async nature of the calls?

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [January 17, 2022, 3:12pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/2 "2022-01-17T15:12:15Z")

</div>

One way could be to have your activity invocations in order in a method of your workflow implementation, for example:

```auto
private void handleSignature(String id, Signature signature) {
      activities.uploadSignature(id, signature);
      activities.submitSignature(id);
}

```

and in your workflow code for example:

```auto
Async.procedure(this::handleSignature, id, signature);

```

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [January 17, 2022, 3:31pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/3 "2022-01-17T15:31:23Z")

</div>

Another way I think could be just to use `thenApply`, for example:

```auto
Async.procedure(activities::uploadSignature, id, signature)
          .thenApply(
              v -> {
                activities.submitSignature(id);
                return v;
              });

```

---

<div class="post-metadata">

**Author:** ![prachi](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@prachi](https://community.temporal.io/u/prachi)\
**Post date:** [January 17, 2022, 11:15pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/4 "2022-01-17T23:15:59Z")

</div>

Thank you @tihomir for the solutions. This works!

---

<div class="post-metadata">

**Author:** ![mschielmann](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mschielmann/32/1712_2.png) [@mschielmann](https://community.temporal.io/u/mschielmann)\
**Post date:** [February 3, 2022, 3:14pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/5 "2022-02-03T15:14:35Z")

</div>

Hi @tihomir, isn’t Activity considered a blocking call?  
I’m asking because API doc of `thenApply` states that: `Note that no blocking calls are allowed inside of the function`.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [February 3, 2022, 8:15pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/6 "2022-02-03T20:15:17Z")

</div>

The api doc that is referred in previous question is [here](https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/main/java/io/temporal/workflow/Promise.java#L120).

We will update the comment to make it more clear. The intent was to say that the function code should follow the same constraints as the rest of the workflow code, outlined [here](https://docs.temporal.io/docs/java/workflows#workflow-implementation-constraints).

So invoking an activity in the function passed to thenApply is fine (blocking and then resuming the workflow thread with things like activity invocation).

Hope that helps.

---

<div class="post-metadata">

**Author:** ![mschielmann](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mschielmann/32/1712_2.png) [@mschielmann](https://community.temporal.io/u/mschielmann)\
**Post date:** [February 3, 2022, 8:26pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/7 "2022-02-03T20:26:38Z")

</div>

Thank you @tihomir . I thought that maybe `thenApply` and `thenCompose` behave differently in that regard, as `thenApply` has this comment about blocking calls, and `thenCompose` does not ([docs](https://github.com/temporalio/sdk-java/blob/master/temporal-sdk/src/main/java/io/temporal/workflow/Promise.java#L141)).  
Thank you for the clarification.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [February 3, 2022, 8:32pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/8 "2022-02-03T20:32:42Z")

</div>

@mschielmann thanks for bringing this up!  
@spikhalskiy already created PR for it!! [https://github.com/temporalio/sdk-java/pull/1029](https://github.com/temporalio/sdk-java/pull/1029)

---

<div class="post-metadata">

**Author:** ![mschielmann](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mschielmann/32/1712_2.png) [@mschielmann](https://community.temporal.io/u/mschielmann)\
**Post date:** [February 3, 2022, 8:37pm UTC](https://community.temporal.io/t/maintaining-order-of-execution-in-async-method-calls/3766/9 "2022-02-03T20:37:07Z")

</div>

@tihomir I really appreciate your fast feedback and reaction!  
I’ve reviewed the MR and added a comment 🙂
