0

I'm trying to find out if a record was kept. From one function I call another to save the record, the problem is the asynchronous request of the second function. To solve it use promises, but I still have the problem of asynchronous when I invoke the promise I can get solved perfectly but my main function does not set the value to the variable isStepValid synchronize.

enterprise.save = function(element){
//some code...
   return new Promise((resolve ,reject) => {
    // .... request service 
     if(response.data[0].status == "SUCESS"){
               resolve(response.data[0].message);
     }
     else{
         reject(response.data[0].message);
      }
  });

}

function validateSteps(stepnumber){ 
var isStepValid = true;
        message="";

                enterprise.save(this).then((resolve)=>{
                isStepValid = true;
                console.log("resolve:",resolve);
                }).catch((reject)=>{
                console.log("reject:",reject);
                isStepValid = false;
                });
}

When this function is finished the variable isStepValid does not match the one of the answer of the promise (It agrees until after executing this function).

Eliuber
  • 48
  • 1
  • 8
  • Promises don't make code synchronous. They just make dealing with asynchronous code a lot easier. – Bergi Aug 11 '17 at 18:05

1 Answers1

0

You cannot make asynchronous function synchronous again.

You should embrace Promises and convert your validateSteps to be an asynchronous function.

mdziekon
  • 3,531
  • 3
  • 22
  • 32