# Return value for workflows

**URL:** <https://community.temporal.io/t/return-value-for-workflows/6965>\
**Category:** Community Support\
**Tags:** typescript-sdk\
**Created:** [January 18, 2023, 6:22pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965 "2023-01-18T18:22:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nareshbhatia](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nareshbhatia/32/3195_2.png) [@nareshbhatia](https://community.temporal.io/u/nareshbhatia)\
**Post date:** [January 18, 2023, 6:22pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/1 "2023-01-18T18:22:59Z")

</div>

Looking at the TypeScript examples, workflows are returning unstructured message strings, usually returned by activities. For example, in nextjs-ecommerce-oneclick:

```auto
export async function OneClickBuy(itemId: string) {
  const itemToBuy = itemId;
  let purchaseState: PurchaseState = 'PURCHASE_PENDING';
  wf.setHandler(cancelPurchase, () => void (purchaseState = 'PURCHASE_CANCELED'));
  wf.setHandler(purchaseStateQuery, () => purchaseState);
  if (await wf.condition(() => purchaseState === 'PURCHASE_CANCELED', '5s')) {
    return await canceledPurchase(itemToBuy);
  } else {
    purchaseState = 'PURCHASE_CONFIRMED';
    return await checkoutItem(itemToBuy);
  }
}

```

Also don’t see how these values are accessed or used on the client side. For example, in the above example, the return value is not accessed or used.

```auto
await client.workflow.start(OneClickBuy, {
  taskQueue: 'ecommerce-oneclick',
  workflowId: transactionId,
  args: [itemId],
});

res.status(200).json({ ok: true });

```

Questions:

1. What are the best practices around determining the return value from a workflow?
2. Would it be a good pattern to at least return the current workflow state so that the client can enable the next set of valid actions. Currently my client is using `await workflow.query('workflowState')` to get the current state, but that seems like an unnecessary round trip. Of course, I realize that the workflow state can change anytime, but this approach might cover some use cases without a round trip.
3. How does the client access the return value (in TypeScript)?

---

<div class="post-metadata">

**Author:** ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)\
**Post date:** [January 20, 2023, 4:59pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/3 "2023-01-20T16:59:07Z")

</div>

Hello @nareshbhatia

> Looking at the TypeScript examples, workflows are returning unstructured message strings, usually returned by activities.

You can return any type of object it has to be serializable. please see:

- [https://github.com/temporalio/samples-typescript/blob/main/ejson/src/client.ts#L32](https://github.com/temporalio/samples-typescript/blob/main/ejson/src/client.ts#L32)
- [Passing functions from activities to workflows - #3 by bergundy](https://community.temporal.io/t/passing-functions-from-activities-to-workflows/5998/3)

> 1. What are the best practices around determining the return value from a workflow?

I think this is closely related to your business. What do you need your workflow function to return?

> 1. Would it be a good pattern to at least return the current workflow state so that the client can enable the next set of valid actions. Currently my client is using `await workflow.query('workflowState')` to get the current state, but that seems like an unnecessary round trip. Of course, I realize that the workflow state can change anytime, but this approach might cover some use cases without a round trip.

To get the internal workflow state the recommendation is to use queries for running workflows. [Build durable applications with Temporal | Temporal Documentation](https://docs.temporal.io/application-development/features?lang=typescript/#queries), and as you have mentioned you can return it as part of the workflow result.

> 1. How does the client access the return value (in TypeScript)?

`const result: Result = await handle.result()`

Have a look at this example [https://github.com/temporalio/samples-typescript/blob/main/ejson/src/client.ts#L32](https://github.com/temporalio/samples-typescript/blob/main/ejson/src/client.ts#L32)

Just in case it helps, with `handle.describe()` you can get other useful information, like the workflow status (completed, running…) : `await handle.describe()).status`

Antonio

---

<div class="post-metadata">

**Author:** ![nareshbhatia](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nareshbhatia/32/3195_2.png) [@nareshbhatia](https://community.temporal.io/u/nareshbhatia)\
**Post date:** [January 20, 2023, 5:48pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/4 "2023-01-20T17:48:48Z")

</div>

Thank you so much, @antonio.perez for a detailed response. Can’t wait to read and try all this. Will follow up, if I have any questions.

---

<div class="post-metadata">

**Author:** ![bergundy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bergundy/32/900_2.png) [@bergundy](https://community.temporal.io/u/bergundy)\
**Post date:** [January 20, 2023, 5:51pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/5 "2023-01-20T17:51:24Z")

</div>

There’s also a `client.workflow.execute` method that’s effectively shorthand for:

```plaintext
const handle = await client.workflow.start(...)
const result = await handle.result()

```

---

<div class="post-metadata">

**Author:** ![nareshbhatia](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nareshbhatia/32/3195_2.png) [@nareshbhatia](https://community.temporal.io/u/nareshbhatia)\
**Post date:** [January 20, 2023, 5:56pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/6 "2023-01-20T17:56:07Z")

</div>

Hi @bergundy, does that amount to a single call to the server or two?

> There’s also a `client.workflow.execute` method that’s effectively shorthand for:
> 
> ```auto
> const handle = await client.workflow.start(...)
> const result = await handle.result()
> 
> ```

---

<div class="post-metadata">

**Author:** ![bergundy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bergundy/32/900_2.png) [@bergundy](https://community.temporal.io/u/bergundy)\
**Post date:** [January 20, 2023, 6:12pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/7 "2023-01-20T18:12:04Z")

</div>

It results in (at least) two server calls.  
It’s just syntactic sugar.

I say “at least” because getting the result is done via long polling the server and it depends how long it takes the workflow to complete.

FTR, if you want to get the result of an already running workflow, you can get a handle to it with:

```plaintext
const handle = client.workflow.getHandle(workflowId)

```

This method does not call the server and thus does not verify that there’s an existing workflow for the given ID.

> **[Class: WorkflowClient | Temporal TypeScript SDK API Reference](https://typescript.temporal.io/api/classes/client.WorkflowClient#gethandle)**
>
> client.WorkflowClient

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [January 20, 2023, 7:05pm UTC](https://community.temporal.io/t/return-value-for-workflows/6965/8 "2023-01-20T19:05:46Z")

</div>

In many use cases, a result of the workflow should be pushed to some external system. In this case, you can use an activity that executes at the end of the workflow and sends the result.
