javascript: async / await – propagation required the entire call chain up? [duplicate]

帅比萌擦擦* 提交于 2019-12-11 03:47:42

问题


I am wrapping a function returning a promises into an async function on a small helper function...

trash(filesArray, {
    glob: false
})
.then(() => {
    resolve(true);
}).catch((err) =>  {
    return err;
})

…because I like to use it synchronous using the await on the next-higher level:

async empty(liveMode, force) {
    …
    await helpers.trashSync(trashFiles);
    // then doing further stuff...
}

Of course, this means, that I need to use the async keyword again... (otherwise I am told await is an ‘unknown reserved’ word or such) and if I am not mistaken, I will now need to use await again on the next-higher level?

await memberSet.empty(true, false)

Does this “game” continue all the way up and throughout my application, so by the end, I have plenty of async/await's wherever there's a tiny async function contained?

Or am I simply missing the point where to stop?


回答1:


You can't convert an async function into a sync function, the purpose of async/await is to allow you to write code that has a similar code path to synchronous calls, but allowing the event loop to be re-entrant and therefore more fine grained. Generator/yield is an alternative approach to interrupting flow of execution.

You'll need to find a point where your call stack naturally expects a returned promise, or doesn't care that it completes asynchronously.



来源:https://stackoverflow.com/questions/49842216/javascript-async-await-propagation-required-the-entire-call-chain-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!