0

I have a recursive function:

let main = () => {
  ftp(_defaultPath, _start, (file, doc, name) => {
    parser(file, doc, name)
  })
}

The parser function:

module.exports = async function (file, doc, name) {
    await funcOne(file, doc)
    await funcTwo(file, doc, name)
    await funcThree(file, doc, name)
}

The callback its called inside the recursive function multiple times:

async function myFuntion(path, name, callback) {
   ...
   callback(file, doc, files[p][1])
   ...
}

The problem is I want to wait when i do callback like:

async function myFuntion(path, name, callback) {
   ...
   await callback(file, doc, files[p][1])
   ... next lines need to wait to finish callback
}

I trying to find how to do that.

It's possible to do that? thanks

2 Answers 2

1

It's possible to do that?

Yeah, it's possible to use await but for this to work:

await callback(file, doc, files[p][1])

your callback() needs to return a promise. From your code it's not clear that it does.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Just i found the solution :D
1

I have done on this way:

I edit my main function with async inside ftp function:

let main = () => {
  ftp(_defaultPath, _start, async (file, doc, name) => {
    await parser(file, doc, name)
  })
}

I added the promise to parser function like that:

module.exports = function (file, doc, name) {
    return new Promise( async (resolve, reject) => {
        try {
            await funcOne(file, doc)
            await funcTwo(file, doc, name)
            await funcThree(file, doc, name)
        } catch(e) {
            return reject(e)
        }
        return resolve()
    }
}

Inside the recursive function I do the await.

await callback(file, doc, files[p][1])

Now waits as expected.

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.