首页 > 解决方案 > NodeJS - 我们应该返回而不是等待以提高性能

问题描述

当考虑到这个 eslint 规则时,看起来应该避免这种代码(即使规则允许它不是“返回等待”时)

async function foo() {
    await bar(); // "cost of an extra microtask before resolving the outer Promise"
} // void return

并改为

async function foo() {
    return bar(); //This may be confusing because there is no real return value
} 

所以问题是——这个“额外的微任务”真的那么重要吗?

显然,在查看 1 个函数时 - 效果可以忽略不计,但如果它在整个代码库中发生很多怎么办?

标签: javascriptnode.jsasynchronousasync-awaitasync.js

解决方案


推荐阅读