Synchronous programming

Synchronous programming

Synchronous programming is a programming model where operations take place sequentially. This model is antithetical to asynchronous programming. In such a model, operations occur one after the other. The program moves to the next step when the current step has completed execution and has returned an outcome. This linear behavior means that long-running operations are "blocking," and the program will halt for the duration it takes to complete them.

How does synchronous programming work?

In synchronous programming, steps defined sequentially occur in the same order. So a program that invokes a synchronous function a() and uses its outcome in a function b() would have to be written in the sequence where a, the primary function, precedes b, the secondary function. This way, by the time the flow of control reaches the secondary function, the outcome of the primary function will be available to the runtime.

 
function a(callback) {
  output = ... // some long operation
  return output
}

function b(input) {
  // do something with input
}

output = a()
b(output)

Write clean and secure code with DeepSource

Powerful static analysis that takes 5 minutes to set up and helps you fix code health and security problems on every pull request.