Asynchronous Programming

Asynchronous Programming

Asynchronous programming is a programming model where operations take place in a non-sequential fashion. This model is antithetical to synchronous programming. In such a model, operations that take time are "non-blocking." Non-blocking operations do not require the runtime to wait for their completion, and the program continues to proceed ahead. Once the operation is complete, either successfully or unsuccessfully, it raises an event that signals that the outcome is ready for any subsequent steps to be performed on it.

How does asynchronous programming work?

In asynchronous programming, steps defined sequentially may occur non-sequentially. So a program that invokes an asynchronous function a() and uses its outcome in a function b() would have to be written so that b, the callback function, can be passed to a, the asynchronous function, as an argument. This way, the long-running function can invoke the callback function after completing its process.


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

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

a(b)

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.