function test() { /*some business logic*/ return response; } function calculate() { if (test() == true) { console.log("Success"); } else { console.log("Fail"); } } my test function is in different js file which does some business processing (processing takes some time) on data and return boolean response.
I am calling test function from calculate function (in different js file). I am expecting console output as 'success'(test function always return true), but it is giving me 'Fail'. But if I debug this code and wait sometimes on '(if(test()==true))' then i get expected output . Basically it is a synchronization issue. How can i solve that?
test()is actually doing people can't help you figure it out.test()is the bit that needs the work. If its making a network request etc you really should look into promises or callbacks instead. JavaScript is single threaded if you hack it up to make a ajax request synchronous you will lock the browser up whilst it waits for the response.