【发布时间】:2019-03-01 04:25:51
【问题描述】:
我一直在阅读不同之处,但在使用有助于异步方法的库时很难考虑。
我正在使用https://github.com/caolan/async 编写特定的异步调用。我很难理解在其中一些异步方法中使用process.nextTick,尤其是异步系列方法,其中异步方法基本上是同步执行的。
例如:
async.waterfall([
next => someAsyncMethod(next),
(res, next) => {
if (res === someCondition) {
return anotherAsyncMethod(next);
}
return process.nextTick(next); // vs just calling next()
},
], cb);
我以前在代码中看到过这种情况。但我不知道为什么?只是调用 next 而不是 process.nextTick 会给我同样的结果吗?
在这些场景中使用process.nextTick 是否有任何理由,其中存在以同步方式控制的异步方法?
另外,像下面这样的异步呢?
async.map(someArray, (item, next) => {
if (item === someCondition) {
return anotherAsyncMethod(next);
}
return process.nextTick(next); // vs just calling next()
}, cb);
谢谢!
【问题讨论】:
标签: node.js