I have this function:
waitForFreeAccnt.prototype.isMemberFree = function () { var self = this; self.api.getMemberInfo(function () { var accType = self.api.connect.accountType; console.log(accType); if (accType === 'FREE') { console.log('it is free'); return true; } else { console.log('it is not free'); return false; } }); }; I would like to wait till the account is free for up to 10 seconds with something like that:
var test = function () { for (var start = 1; start < 10; start++) { var result = self.isMemberFree(); console.log(result); if (result) { break; } else { self.api.pause(1000); console.log('waiting'); } } }; But it doesn't work because self.api.getMemberInfo is asynch call. This is super frustrating with Javascript. Any other language it would be so simple to do. How do I force the for loop to wait for self.isMemberFree() to finish executing before proceeding with the loop?
Also to note, this is not in browser execution so I don't care about anything hanging.
self.api.connectreturn some kind of deferred object or promise? You should wait for the object to be resolved before moving on to th next item..setTimeout()orsetInterval()and put your next block of code in a function that you can call from within thegetMemberInfo()callback.