liveBook · Manning

Fulfilled state

As explained in API Design Patterns

Resolution is the successful completion of an asynchronous computation with its final value available.

A fulfilled state marks the successful completion of an asynchronous computation, with its final value available. For a long-running operation, reaching this state means determining when the operation is done and retrieving its result. section 10.3.3

Use one of two common ways to determine whether an operation has fulfilled:

  • Poll the operation with GetOperation until its done field becomes true, then return the result. This is straightforward but can generate repeated requests and unnecessary network and server work. section 10.3.3
  • Call a custom WaitOperation method that waits for completion before returning the final result, reducing client-side polling logic. section 10.3.3
Listing 10.5 Polling for updates about an LRO until it’s done
async function createChatRoomAndWait(): Promise<ChatRoom> {
  let operation: Operation<ChatRoom, CreateChatRoomMetadata>;
  operation = CreateChatRoom({                                 #1
    resource: { title: "Cool Chat" }
  });
 
  while (!operation.done) {                                    #2
    operation = GetOperation<ChatRoom, 
      CreateChatRoomMetadata>({                              #3
      id: operation.id
    });
    await new Promise( (resolve) => {                          #4
      setTimeout(resolve, 1000)
    });
  }
  
  return operation.result as ChatRoom;                         #5
}

The listings show a CreateChatRoom operation being resolved through polling, a standard GetOperation method, or a custom WaitOperation method. section 10.3.3

How API Design Patterns covers it

How deep it goes
Explains polling, completion flags, request costs, and custom waiting, but does not detail rejection handling.
Examples
Uses CreateChatRoom, GetOperation, WaitOperation, Operation resources, and a ChatRoom result.
What it assumes
Assumes asynchronous methods can return operation resources and distinguishes successful resolution from other promise states.
What you can do after
Operate long-running API calls by polling an operation or invoking a custom wait method before reading its result.
How it connects
Connects promises, asynchronous computation, long-running operations, polling, and web APIs.

Also explained in

Compare how 2 books explain fulfilled state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.