asynchronouslanguage-lawyerumlsequence-diagram

Asynchronous replies in UML sequence diagrams (async/await pattern)


Many modern languages promote asynchronous calls, with a way to wait for the result when it's really needed. Example: async/await in C# and Swift or promise/future in C++. Take the following hypothetical implementation :

class X { 
  private Y y; 
  public async void doSomething() {
    var someTask = y.doSomeAsyncOp();// Asynchronous call 
    doSomethingImmediately();        // Potentially in parallel with async behavior
    var result = await someTask;     // Getting or waiting for result of async call
  }
  ...
}

This implements the intent of a reply for the async call in the design:

sequence diagram with async call and async reply

Unfortunately, according to the UML 2.5.1, reply messages seem to be meant only for synchronous calls and asynchronous calls are not supposed to have replies. See section 17.4.3:

If the messageSort is reply, then the Message represents the return from a synchronous call to the Operation. The arguments of the Message correspond to the out, inout and return ownedParameters of the Operation (...).

Section 13 of the specifications, which defines synchronous and asynchronous behavior, is consistent with this understanding. So, how to model the design with legit UML?


Solution

  • Even though the async/await pattern is used for asynchroneous communication, both the call to the operation and the await have return values and the sender has to wait for them. Therefore, I would suggest to use synchroneous calls.

    example sequence diagram

    The connection between the two messages is the assignment target someTask of the first operation used as the argument of the await.