Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  171] [ 1]  / answers: 1 / hits: 9020  / 9 Years ago, sun, september 27, 2015, 12:00:00

if function returns error, further code is no longer executing. I need to retry this function until success. How can I do it?



... // API request...

function(error, something) {
if (!error) {
something = true;
// Etc...
}
else {
// Code to try again.
}
}

More From » node.js

 Answers
11

Try this



  do {
// do your stuff here
}while(error)


For tour case you can do it like this :



function(error, something) {
do {
// do your stuff here
}while(error)
}


To do what you want until the error become false



Or you can use while



    function(error, something) {
if(!error){
// this code is executed once
}
while(error){
// do your stuff here
}
}


It will test the error before executing the first time



For more example take a look here



For the last comment you can do it like this (without loop) :



function Test(error, something) {
if(!error){
// your code that you want to execute it once
}
else {
// do stuff
Test(error, something); // re-call the function to test the if
}
}

[#33887] Friday, September 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rociom

Total Points: 287
Total Questions: 88
Total Answers: 101

Location: Oman
Member since Wed, Nov 17, 2021
3 Years ago
;