简体   繁体   English

从 javascript 异步 function 获取状态回调

[英]Get status callback from a javascript async function

I'm not so skilled on JS.我对JS不太熟练。

I'm importing a function from an external lib我正在从外部库导入 function

import { doSomething } from '@/javascript/solana.ts';

Into this lib execute is defined in this way..进入这个lib执行是这样定义的..

export async function doSomething(...) { 
 ....
 try {
  ... 
} catch (e) {
  ...  
}
}

In my code I need to catch for error and for success.在我的代码中,我需要捕捉错误和成功。

I have a function that call my imported function我有一个 function 调用我进口的 function

function drawImages() {
  doSomething( ... ).catch(e => {
    console.log('There has been a problem with your fetch operation: ' + e.message);
  });
}

How can I manage the success?我该如何管理成功?

Use Promise#then to handle the value returned when it is successful.使用Promise#then处理成功时返回的值。

function drawImages() {
  doSomething( /* ... */ ).then(result => console.log(result)).catch(e => {
    console.log('There has been a problem with your fetch operation: ' + e.message);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM