# Example of using Workflow newTimer function

**URL:** <https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [February 1, 2023, 8:13pm UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127 "2023-02-01T20:13:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![averri](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/averri/32/2742_2.png) [@averri](https://community.temporal.io/u/averri)\
**Post date:** [February 1, 2023, 8:13pm UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127/1 "2023-02-01T20:13:17Z")

</div>

Hello, I’m looking for an example of using the Workflow _newTimer_ function. I didn’t find anything about it in the temporal-java-samples project. Ideally, this response should include a simple example using the Java SDK, but if you know the answer using any other SDK, I appreciate your help.

The context, suppose the following use case:

1. Send an email to the user with a validation token.
2. Set a timer to cancel the workflow if the token is not confirmed in a specific amount of time.

Implementing the interaction with the user is very simple, we just need to use signals. I don’t know how to set the timer in item (2).

---

<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:** [February 2, 2023, 12:45am UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127/2 "2023-02-02T00:45:23Z")

</div>

You don’t need `newTimer` for this use case. I would use `workflow.await` instead:

```auto
activities.sendEmail(validationToken);
// confirmationReceived is set by the signal handler
if (workflow.await(confirmationTimeout, ()->confirmationReceived)) {
 // handle confirmation
} else {
 // handle timed out
}

```

---

<div class="post-metadata">

**Author:** ![averri](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/averri/32/2742_2.png) [@averri](https://community.temporal.io/u/averri)\
**Post date:** [February 2, 2023, 12:57pm UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127/3 "2023-02-02T12:57:39Z")

</div>

Hi @maxim, thank you so much for the information. That worked very well. When gracefully handling the timeouts like in the example you gave, the workflow is marked as “Completed”, and I think that is the right thing to do because the workflow has completed its job, with an outcome of “Confirmation timeout”.

I’m returning the status in the workflow method so that we can see if there was a timeout or not:

```auto
activities.sendEmail(validationToken);
// confirmationReceived is set by the signal handler
if (workflow.await(confirmationTimeout, ()->confirmationReceived)) {
 // handle confirmation
 return "Success"
} else {
 return "Confirmation timeout"
}

```

---

<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 2, 2023, 2:47pm UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127/4 "2023-02-02T14:47:19Z")

</div>

```auto
if (workflow.await(confirmationTimeout, ()->confirmationReceived)) {
 // handle confirmation
 return "Success"
} else {
 return "Confirmation timeout"
}

```

I think another option could be to check if confirmationReceived is true/false as  
the workflow.await says "wait until either the confirmationTimeout fires or confirmationReceived becomes true.

So something like

```auto

workflow.await(confirmationTimeout, ()->confirmationReceived)
if(confirmationReceived) { ... success ...} else { ...timeout ...}

```

---

<div class="post-metadata">

**Author:** ![averri](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/averri/32/2742_2.png) [@averri](https://community.temporal.io/u/averri)\
**Post date:** [February 2, 2023, 5:13pm UTC](https://community.temporal.io/t/example-of-using-workflow-newtimer-function/7127/5 "2023-02-02T17:13:40Z")

</div>

Thanks for your suggestion.
