如何使用承诺执行同步函数还是同步函数,如何有效地调用通用函数?
无论调用同步函数,还是调用异步函数,我都需要调用一个共同的函数。
这就是我首次写的方式,但这是不正确的,因为 foo
将在均匀函数完成之前调用。
function main(element) {
if (element.id === 'sync') {
syncFunction();
} else {
asyncFunction();
}
foo();
}
因此,我想到了这一点,但是在这里,我觉得我正在重复 foo
的电话。有更好的方法吗?
function main(element) {
if (element.id === 'sync') {
syncFunction();
foo();
} else {
asyncFunction().then(() => {
foo();
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将呼叫的结果放入
Promise.resolve()
中,则可以无条件地将.。
If you put the result of the call into a
Promise.resolve()
, you can chain.then
onto it unconditionally.