R
R
retyui2017-01-21 16:17:24
Node.js
retyui, 2017-01-21 16:17:24

Node.js recursive output of an async function?

There is an asynchronous function! which reads files, parses them, then checks for a link to the parent file, and recursively runs through the dependency chain ... replenishing the stack.
example below (code is not valid, but readable)

let getParent(file, cb)=>{
  readFile(file , (err,data)=>{
    parse(data, (err, jsObject)=>{
      cb(jsObject.parent);
    })
  })
};
let stack = []
getParent('main.file'(err,parent)=>{
  if(!parent){return;}
  stack.push(parent);
  getParent(parent(err,parent)=>{
    if(!parent){return;}    
    stack.push(parent);
      //getParent callback hell
    })
})

Condition: I don't know how long the dive chain is , the recursive pass helped a lot,
but how to implement this with asynchronous functions ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-01-22
@vitali1995

You need to move the callback to a named function and pass it as an argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question