pub fn promise_yield_create(
function_name: &str,
arguments: &[u8],
gas: Gas,
weight: GasWeight,
register_id: u64,
) -> PromiseIndex
Expand description
Creates a promise that will execute a method on the current account with given arguments. Writes a resumption token (data id) to the specified register. The callback method will execute after promise_yield_resume is called with the data id OR enough blocks have passed. The timeout length is specified as a protocol-level parameter yield_timeout_length_in_blocks = 200.
The callback method will execute with a single promise input. Input will either be a payload provided by the user when calling promise_yield_resume, or a PromiseError in case of timeout.
Resumption tokens are specific to the local account; promise_yield_resume must be called from a method of the same contract.
ยงExamples
use near_sdk::env::{promise_yield_create, promise_yield_resume, read_register};
use near_sdk::serde_json;
use near_sdk::{Gas, GasWeight, CryptoHash};
let DATA_ID_REGISTER = 0;
// Create yield promise
let promise = promise_yield_create(
"increment",
// passed as arguments
serde_json::json!({
"value": 5
}).to_string().into_bytes().as_slice(),
Gas::from_tgas(10),
GasWeight(0),
DATA_ID_REGISTER
);
// Retrieve `data_id` for further resume
let data_id: CryptoHash = read_register(DATA_ID_REGISTER)
.expect("read_register failed")
.try_into()
.expect("conversion to CryptoHash failed");
// Resume execution using previously retrieved `data_id`
promise_yield_resume(
&data_id,
// passed as callback_result
serde_json::json!({
"key": "value",
"description": "some text"
}).to_string().into_bytes().as_slice()
);
More low-level info here: near_vm_runner::logic::VMLogic::promise_yield_create
See example of usage here and here