0

Still relatively new to Node and it's non-blocking nature.

I have written an API module for the backend of a desired desktop application I want to make. When each method is called, it returns an object. Two examples are below.

{ success: 1, response: RESPONSE }
{ success: 0, msg: ERROR_MSG }

1 being a successful operation, 0 not.

What I want to do is now start linking the backend API to a main program. I want to call a function from the module and if my success code is 1, move on to the next operation, if 0 wait a desired time (1000ms) and retry the operation, all whilst not blocking the event loop.

Thanks.

By Styl
  • 21
  • 3
  • It's not clear what part of the project you are asking about. Is this about code in the browser calling the api via something like fetch, of is it backend code using a different async backend module? A little code will make answering much easier. – Mark Oct 29 '17 at 20:37
  • @Mark_M Can't put much code public here. Reading back upon my initial question I was a little vague. I am writing a Desktop application, currently developing purely as a CLI, and will eventually move to an Electron application. Right now, I have the 'backend API' written as a module which I am requiring and calling methods from in my index file. I want to call a method, if the response is a success, move on, if not, re-call the method after 1000ms delay, without blocking the main event loop. – By Styl Oct 29 '17 at 20:44
  • @Mark_M does that make more sense? – By Styl Oct 29 '17 at 21:51
  • Without showing us your specific problem and code, your question is just a generic question about retrying an async operation so it's been marked a dup of one of the other questions on that topic. – jfriend00 Oct 30 '17 at 03:53

1 Answers1

1

There are so many ways to do this and they all depend on your specific situation. Since you can't post code, the best we can do is a stab in the dark. Here is an async function that returns your object. It returns a failed object roughly 3/4 of the time. Next we can call this function every second until it returns a success with a simple setInterval:

function asyncFn() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const options = [{
          success: true,
          response: 'RESPONSE'
        },
        {
          success: false,
          msg: 'ERROR_MSG'
        }
      ]
      resolve(Math.random() > .75 ? options[0] : options[1])
    })
  })
}


const interval = setInterval(() => {
  asyncFn().then(result => {
    if (!result.success) {
      console.log("failed: ", result)
      return
    }
    clearInterval(interval)
    console.log("success: ", result)
  })
}, 1000)

If you're code is significantly different, then you'll probably need to figure out how to post some code or create a minimal example.

Mark
  • 90,562
  • 7
  • 108
  • 148
  • Thanks, with a bit of tinkering I managed to get it working. However, when I seem to be having a weird issue where the code executes more times than it'd be expected to. So when mapping tasks using Async Parallel, the first promise works fine, but the program progresses to the next promise, it runs extra iterations. Been trying to debug all day... – By Styl Oct 30 '17 at 17:37