Synchronous Programming

What is Synchronous Programming?

  • In a synchronous programming model, things happen one at a time

  • This means that only one operation can be in progress at time

  • When you call a function that performs a long-running action, it returns only when the action has finished and it can return the result

  • Subsequent actions can only be run after the previous action has completed; this means that action #1 blocks action #2

  • JavaScript is a synchronous, blocking, single-threaded language by default

Most of the code we’ve written so far has been synchronous


Example

Here’s an example of Synchronous code

  function printLetter(letter) {
    console.log(letter)
  }

  function printAll(){
    printLetter("A")
    printLetter("B")
    printLetter("C")
  }

  printAll()

The the example above, printAll() calls printLetter() 3 times with “A”, “B”, “C” as parameters and each letter is printed out in the order in which the function was called

JS Bin on jsbin.com