A
A
Axepec2017-04-17 20:01:18
Node.js
Axepec, 2017-04-17 20:01:18

Async in node.js, eachOfSeries synchronous loop, or doing only one iteration?

Good day, professors.
I'm new to node, I'm trying to make a synchronous loop in node. async library.
Hangs after the first iteration. Example:

res = [
  'textFile/file_1.txt'  # ООООЧень большой
  'textFile/file_2.txt'  # маленький
  'textFile/file_3.txt'  # ооочень маленький
  ]

funRead = (file, i) ->
  fs.readFile file, 'utf8', (err, contents) ->
    console.log i + ') i am read ->> ' + file

async.eachOfSeries res, ((item, i) ->
  funRead(item, i)
  ), ->
    console.log 'testCB'

result: 0) i am read ->> textFile/file_1.txt The callback
is not executed.......
if you use async.eachOfthe result naturally like this:
1) i am read ->> textFile/file_2.txt
2) i am read ->> textFile/file_3.txt
0) i am read ->> textFile/file_1.txt The callback
is not executed....... The point is
not to stop the node, but not to start a new iteration, until the old one finishes working ..... Day 3 went hopelessly searching for a solution ....
Or here is an option for three functions, you need to do it sequentially:
async.series [
  funRead1 = () ->
    fs.readFile 'textFile/file_1.txt', 'utf8', (err, contents) ->
      console.log '1) i am read ->> textFile/file_1.txt'
  funRead2 = () ->
    fs.readFile 'textFile/file_2.txt', 'utf8', (err, contents) ->
      console.log '2) i am read ->> textFile/file_2.txt'
  funRead3 = () ->
    fs.readFile 'textFile/file_3.txt', 'utf8', (err, contents) ->
      console.log '3) i am read ->> textFile/file_3.txt'
], () ->
  console.log 'test CB'

Outputs: 1) i am read ->> textFile/file_1.txt
and closes.... can't figure it out, HELP

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dummyman, 2017-04-18
@Axepec

Bro, well, I don’t know what’s in res ,
but there’s definitely nothing asynchronous in func() , so in a specific example, you can use at least a loopfor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question