MCPcopy Index your code
hub / github.com/RustPython/RustPython / then

Method then

crates/wasm/src/js_module.rs:471–532  ·  view source on GitHub ↗
(
            &self,
            on_fulfill: OptionalOption<ArgCallable>,
            on_reject: OptionalOption<ArgCallable>,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

469
470 #[pymethod]
471 fn then(
472 &self,
473 on_fulfill: OptionalOption<ArgCallable>,
474 on_reject: OptionalOption<ArgCallable>,
475 vm: &VirtualMachine,
476 ) -> PyResult<PyPromise> {
477 let (on_fulfill, on_reject) = (on_fulfill.flatten(), on_reject.flatten());
478 if on_fulfill.is_none() && on_reject.is_none() {
479 return Ok(self.clone());
480 }
481 match &self.value {
482 PromiseKind::Js(prom) => {
483 let weak_vm = weak_vm(vm);
484 let prom = JsFuture::from(prom.clone());
485
486 let ret_future = async move {
487 let stored_vm = &weak_vm
488 .upgrade()
489 .expect("that the vm is valid when the promise resolves");
490 let res = prom.await;
491 match res {
492 Ok(val) => match on_fulfill {
493 Some(on_fulfill) => stored_vm.interp.enter(move |vm| {
494 let val = convert::js_to_py(vm, val);
495 let res = on_fulfill.invoke((val,), vm);
496 convert::pyresult_to_js_result(vm, res)
497 }),
498 None => Ok(val),
499 },
500 Err(err) => match on_reject {
501 Some(on_reject) => stored_vm.interp.enter(move |vm| {
502 let err = new_js_error(vm, err);
503 let res = on_reject.invoke((err,), vm);
504 convert::pyresult_to_js_result(vm, res)
505 }),
506 None => Err(err),
507 },
508 }
509 };
510
511 Ok(PyPromise::from_future(ret_future))
512 }
513 PromiseKind::PyProm { then } => Self::cast_result(
514 then.call(
515 (
516 on_fulfill.map(IntoObject::into_object),
517 on_reject.map(IntoObject::into_object),
518 ),
519 vm,
520 ),
521 vm,
522 ),
523 PromiseKind::PyResolved(res) => match on_fulfill {
524 Some(resolve) => Self::cast_result(resolve.invoke((res.clone(),), vm), vm),
525 None => Ok(self.clone()),
526 },
527 PromiseKind::PyRejected(err) => match on_reject {
528 Some(reject) => Self::cast_result(reject.invoke((err.clone(),), vm), vm),

Callers 1

catchMethod · 0.45

Calls 13

weak_vmFunction · 0.85
js_to_pyFunction · 0.85
pyresult_to_js_resultFunction · 0.85
new_js_errorFunction · 0.85
ErrClass · 0.50
flattenMethod · 0.45
is_noneMethod · 0.45
cloneMethod · 0.45
upgradeMethod · 0.45
enterMethod · 0.45
invokeMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected