Skip to content

Blocking function vs Suspending function

Devrath edited this page Feb 5, 2024 · 4 revisions

Blocking Function

  • Blocking functions are those, whose execution will block the program as a whole and will not continue until the blocking function has completed its execution.
  • Other tasks cannot be executed until the blocking function is complete.
  • The Blocking function blocks the thread on which it is executing.

Suspending Function

  • Suspending functions are those, whose execution will not block the execution of the main function.
  • Other tasks can be executed until the suspending function is complete.
  • The Suspending function will not block the thread it executes.
  • NOTE: A suspending function can only be called from a co-routine

If the suspending function can only be called from another suspending function, How can the first suspending be called? πŸ€”

  • We need to use the co-routine builder for this

Summary

In summary, the key difference lies in the impact on the program's execution flow. Blocking functions bring the entire program to a halt until the operation is complete while suspending functions allow other tasks to continue while waiting for the completion of an asynchronous operation.

Clone this wiki locally