Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I always thought async functions need to contain "await" keyword inside, for example:(我一直认为异步函数需要在其中包含“ await”关键字,例如:)

async function doTask() { let total = await CustomFunction(values); console.log(`Main Total: ${total}`); } doTask(); But I also saw some like that define async function without having await as:(但我也看到了一些类似的定义异步功能而不必等待的情况:) GetData = async ( method, url, params) => { Axios.request({ method, url, params }); } so what's the point to have async keyword added in front of a function that doesn't have "await" keyword?(那么在没有“ await”关键字的函数前面添加async关键字有什么意义呢?)   ask by secondimage translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
374 views
Welcome To Ask or Share your Answers For Others

1 Answer

Well in your example it probably is a bug;(在您的示例中,它可能是一个错误;)

I guess there should be a return await in front of Axios.request .(我猜应该在Axios.request前面有一个return awaitAxios.request 。) But in general, unless the caller explicitly only accepts functions returning a Promise, there is no point in declaring an await-less function async.(但是通常,除非调用者显式地仅接受返回Promise的函数,否则声明无等待函数异步是没有意义的。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...