gpt4 book ai didi

node.js - 如何使用 Node.js 嵌套异步 forEachSeries 循环

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:30 25 4
gpt4 key购买 nike

使用coffeescript进行编码 One solution | see this post

我有两个循环。我想从数组“a”中获取每个值,然后循环遍历“b”的所有值处理它们并移动到数组“a”中的下一个值

预期输出:

1 a b c
2 a b c
3 a b c

我看到的错误:

 [ 1, 2, 3 ]

[ 'a', 'b', 'c' ]

1
2
3

TypeError: Cannot read property 'length' of undefined
at Object.forEachSeries(~/src/node_modules/async/lib/async.js:103:17)




Async = require('async')

@a = [1,2,3]
@b = ['a','b','c']

console.dir @a
console.dir @b

Async.forEachSeries @a, (aa , cbLoop1) ->
console.log aa
cbLoop1()
Async.forEachSeries @b, (bb , cbLoop2) ->
#here will be some callback that I need to process before moving to next value in
#b array
console.log bb
cbLoop2()

最佳答案

您的第一个 Async.forEachSeries 调用需要一个回调,该回调会更改 this 的值

Async.forEachSeries @a, (aa , cbLoop1) ->
# inside the callback, the value of `this` has changed,
# so @b is undefined!

使用 => 语法将 this 的值保留在回调中:

Async.forEachSeries @a, (aa , cbLoop1) =>
console.log aa
cbLoop1()
Async.forEachSeries @b, (bb , cbLoop2) ->
# use `=>` again for this callback if you need to access this (@) inside
# this callback as well

关于node.js - 如何使用 Node.js 嵌套异步 forEachSeries 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com