I have been experimenting with the ledger.exercise function
Is there an example available for the following type of react structure ?
const [choiceReturnValue, events] = await ledger.exercise (ContractChoice, contractId, choiceArguments)
How can this used within a
Async... Try... Await ... Catch
structure ?
what is the events
const used for ?
1 Like
exercise
returns a Promise that resolves to the return value of the choice and a list of events that were created as a choice. E.g., consider a choice that archives the current contract and creates a new one. Then you will see 2 events: One being the archive of the current contract and the other being the create event for the new contract. If you don’t need the events then you can just ignore them. This is fairly common in particular if you’re listening on the streaming APIs and just need to get the current state while not having the need to associate events with the exercise that produced them.
Here’s a small example of a try-catch
. There isn’t anything DAML-specific here so I recommend consulting general JS resources, e.g., https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await#Adding_error_handling if you need more examples.
async function exerciseCatch() {
try {
const [result, events] = await ledger.exercise(…);
return result;
} catch(e) {
// Just print out the error to the console. In a real application you probably want to present it to the user.
console.log(e);
return e;
}
3 Likes