Lucas Lemos
Lucas Lemos

Reputation: 63

what is ContractPromise? near-sdk-as to near-sdk-rs comparison

I've found this function in an assemblyscript project for a NEAR contract:

export function assert_single_promise_success(): void {
  const x = ContractPromise.getResults()
  assert(x.length == 1, "Expected exactly one promise result")
  assert(x[0].succeeded, "Expected PromiseStatus to be successful")
}

What does ContractPromise.getResults() do exactly? How should implement the same thing in rust?

Upvotes: 2

Views: 184

Answers (2)

John
John

Reputation: 11429

I'm going to give an answer, comments taken directly from the implementation of ContractPromise.getResults(), which can be found here. The implementation also has an example on how to use the function, which may be useful.

Method to receive async (one or multiple) results from the remote contract in the callback.

@returns An array of results based on the number of promises the callback was created on. If the callback using then was scheduled only on one result, then one result will be returned.

Upvotes: 1

amgando
amgando

Reputation: 1591

here is something similar in Rust from one of the examples in the SDK repo

require!(env::promise_results_count() == 2);

let data0: Vec<u8> = match env::promise_result(0) {
  PromiseResult::Successful(x) => x,
  _ => env::panic_str("Promise with index 0 failed"),
};

Upvotes: 2

Related Questions