在主文件中调用异步函数 [英] Calling async function in main file

查看:53
本文介绍了在主文件中调用异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建集成两个系统的Ap​​p.因此,我正在使用一些请求和异步功能.在异步函数中调用异步函数没有问题.但是,我需要以某种方式结束该链,并在从中提供应用程序的主文件中调用异步函数. 你有什么想法吗?部分代码看起来像这样

I am creating App integrating two systems. Therefore, I am using some requests and async functions. It's no problem to call async function in async function. However, I need to end somehow this chain and call async function in my main file where is App served from. Do you have any idea how to do it? Part of code looks like this

async function asyncFunctionINeedToCall() {
  await childAsyncFunction()
}

asyncFunctionINeedToCall()

谢谢您的回答!

推荐答案

由于主作用域不是async,因此您需要执行一个异步匿名函数来调用函数和自身:

Since the main scope is not async, you will need to do an async anonymous function that calls your function and itself :

(async function() {
  await yourFunction();
})();

或兑现承诺:

yourFunction().then(result => {
  // ...
}).catch(error => {
  // if you have an error
})

希望有帮助,
最好的问候

Hope it helps,
Best regards

这篇关于在主文件中调用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆