orElse

orElse<T, R>(previous: TryResult<T>, orElse: () -> R): T | R

Returns the result of the orElse argument if the previous argument to try fails. Otherwise, the function returns the value of the previous argument.

Introduced in DataWeave: 2.2.0.

Parameters

Name Description

previous

Result from a previous call to try.

orElse

Argument to return if the previous argument fails.

Example

This example waits shows how to chain different try

Source

%dw 2.0
import * from dw::Runtime
var user = {}
var otherUser = {name: "DW"}
output application/json
---
{
    a: try(() -> user.name!) orElse "No User Name",
    b: try(() -> otherUser.name) orElse "No User Name"
}

Output

{
  "a": "No User Name",
  "b": "DW"
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub