Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ioiofadhil's avatar

How to make function javascript synchronous?

Hi, im learning code by my self. And i skipped most of basic things in learning JavaScript. Today, im facing a problem.

I created 2 functions. Inside one of them contains ajax. The javascript execute the function in the same time. Example :

function functionOne() {
			// ajax
			console.log(1)
}

function functionTwo() {
			// do html append or whatsover
			console.log(2)
}

i called them in a jQuery function like this :

$(document).ready(function() {
			functionOne();
			functionTwo();
}

result :

2
1

i want them to be executed "in order". How can you do that? (Except putting the functionOne inside the functionTwo, because the writing sometimes will be weird and hard to read).

0 likes
1 reply
MohamedTammam's avatar

Use async for your AJAX call.

async function functionOne() {
			await $.ajax // ...
			console.log(1)
}

function functionTwo() {
			// do html append or whatsover
			console.log(2)
}

Please or to participate in this conversation.