1
dUcKtYpEd
34d

JavaScript: can you recursively call and sync method? How’s that work?

Comments
  • 1
    Could you elaborate? :)
  • 1
    @ScriptCoded I misspelled. I need to within an asynchronous method, call on itself over and over again until it gets the value i need in the http response
  • 1
    Do it with promises.
  • 1
    I like Rx/RFP for this (retry) and most other types of logic. You can easily encapsulate all the necessary behaviors to ensure safe execution of this sort of thing into a single reusable machine.

    Ex: https://gist.github.com/FailedCheck...

    Note: normally I have a large library of custom operators we would use to solve this problem in less space, but the essence of it is here.
  • 0
    @dUcKtYpEd Aah. Using async/await?
  • 0
    Yeah you can, even without async/await.

    According to Marc Wandschneider's node book it's the recommended way to (e.g.) iterate over an array of fs paths and apply an async function to each of them. But that was a particular situation
  • 0
    @dmonkey so I have iterated over FS paths before by creating a promise for every single one of them, storing each promise in an array, returning the array and calling Promise.all on the array.

    I haven’t ever recursively called an async function before though. I’m guessing I would just need to await the result before calling the recursion again but as soon as I call the recursion, the context of my async call and what I would be awaiting is lost. That’s what confuses me. I’m gonna have to sandbox this tonight.
  • 0
    @SortOfTested thanks for uploading that I’m going to digest this some. You take a very different approach with your function choices. I don’t use pipe or swapMaps too often
  • 0
    @SortOfTested oh wait it’s cause your using RX ok
Add Comment