Definitions
Synchronous or asynchronous relates to the execution flow of a program. When an operation executes completely before passing control to the next, the execution is synchronous. This is the standard method of code execution - in the languages I know, and I imagine also in most of the languages I don’t know.
When one or more operations are time consuming, it may be interesting to run them asynchronous, so that the rest of the code can be executed without waiting for them to finish. In this case, the code following the command that triggers the asynchronous operation cannot count on the result of this operation, of course. Everything that depends on the result of the operation needs to be done only when it has been completed, and usually this occurs in a callback, that is, a code block (usually a function or method) reported to the command that starts the asynchronous operation.
Languages can implement asynchronism in different ways. Usually this is done with Threads and event loops, as in Javascript.
Examples
In Javascript, in the browser, the classic case of asynchronous operation is AJAX - English acronym for Javascript and XML asynchronous. We call AJAX the requests made to a server from JS on a web page. For example, with jQuery to abbreviate:
$.get('http://example.com', funcaoQueExecutaQuandoRespostaChegar);
// o código seguinte executa antes da resposta da requisição acima
fazAlgumaCoisa();
// e a declaração do callback
function funcaoQueExecutaQuandoRespostaChegar(resposta) {
// a resposta não pode ser usada fora daqui,
// a menos que você a passe, a partir daqui,
// para uma outra função
}
As the request is potentially time consuming (and certainly longer than any local operation), if it is done synchronously the page will be frozen until the answer arrives. Therefore it is recommended to use AJAX and callbacks in such cases.
Another typical example occurs in the Desktop application user interface. If the program wants to show a progress bar indicating the progress of a time-consuming operation, it must necessarily use asynchrony. Otherwise the interface can only update the progress bar once at the end of the operation - which would make no sense for a progress bar!
See if the drawing in this answer helps http://answall.com/a/45721/3635 :)
– Guilherme Nascimento
@Guilhermenascimento I added to the references, thank you for your contribution, and congratulations on your good answer to this question.
– Bruno Costa