Continuation passing style
The preceding code, in which you call a function but also pass another function that is to be executed when the input/output operation is finished, can be considered a case of continuation passing style (CPS). What is this technique of coding? One way of looking at it is by thinking about the question: how would you program if using the return statement was forbidden?
At first glance, this may appear to be an impossible situation. We can get out of our fix, however, by passing a callback to the called function, so that when that procedure is ready to return to the caller, instead of actually returning, it invokes the passed callback. By doing this, the callback provides the called function with the way to continue ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access