GetOperation retrieves an operation resource by identifier so clients can determine whether a long-running operation is complete, inspect its result, or monitor its metadata. It should distinguish retrieval failures—such as missing or inaccessible operation resources—from errors produced by the operation itself. section 10.3.3 section 10.3.4
Use the method with these design rules:
- Return the operation resource, including its done state, result, and typed metadata. section 10.3.3
- Let clients control polling frequency and decide when to stop; repeated requests trade network and compute cost for shorter completion delay. section 10.3.3
- Throw an HTTP error only when retrieving the operation fails; represent an underlying operation failure inside the retrieved result as an OperationError. section 10.3.4
- Give OperationError a unique machine-readable code, a human-readable message, and structured details; do not require clients to parse message text. section 10.3.4
Polling until a chat room is created
CreateChatRoom returns an operation. The client repeatedly calls GetOperation with operation.id, waits between requests, and returns operation.result when done is true. A client library may perform this polling automatically, optionally using exponential back-off. 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
}
Follow the complete polling loop, including the operation identifier, one-second delay, completion check, and result extraction. section 10.3.3
Listing 10.9 Checking for an error or returning the result
function isOperationError(result: any):
result is OperationError { #1
const err = result as OperationError;
return err.code !== undefined && err.message !== undefined;
}
function createChatRoomAndWait(): Promise<ChatRoom> {
let operation: Operation<ChatRoom, CreateChatRoomMetadata>;
operation = CreateChatRoom( { resource: { title: "Cool Chat" } });
operation = WaitOperation({ id: operation.id });
if (isOperationError(operation.result)) { #2
// ...
} else {
return operation.result as ChatRoom;
}
}
Follow the client-side distinction between an OperationError result and a successful resource result. section 10.3.4
