Contact Us 1-800-596-4880

orElse

orElse<T, E, R>(previous: Result<T, E>, 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 version 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"
}