【发布时间】:2020-01-17 00:43:02
【问题描述】:
我有一个名为noNCreatedResources 的数组。我想对数组的每个项目进行一些操作,并将项目推入createdResources 数组并从noNCreatedResources 数组中删除该项目并继续这样做,直到noNCreatedResources 为空。为此,我编写了 CreateResources 函数,包括嵌套的 while 和 for 循环。它工作正常,但我意识到它不能同步工作。例如:它必须在 while 循环中迭代两次,但迭代 4 次,我不知道为什么。
我想我不明白 node.js 的异步/等待/非阻塞概念的概念。任何机构都可以帮助我了解问题所在吗?
CreateResources = async () => {
while (this.noNCreatedResources.length > 0) {
for (let index = 0; index < this.noNCreatedResources.length; index++) {
if (this.resourceHasCreatedDependencies(this.noNCreatedResources[index])) {
const resourceModel = this.someOperation(this.noNCreatedResources[index]);
this.createdResources.push(resourceModel);
this.noNCreatedResources.splice(index, 1);
}
}
}
}
【问题讨论】:
标签: node.js asynchronous async-await nested nonblocking