[javascript] How to make code wait while calling asynchronous calls like Ajax