Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Callbacks and higher-order functions

In TypeScript, functions can be passed as arguments to another function. Functions can also be returned by another function. A function passed to another as an argument is known as a callback. Functions that accept functions as parameters (callbacks) or return functions are known as higher-order functions.

Callbacks are usually anonymous functions. They can be declared before they are passed to the higher-order function, as demonstrated by the following example:

var foo = function() { // callback 
  console.log("foo"); 
} 
 
function bar(cb: () => void) { // higher order function 
  console.log("bar"); 
  cb(); 
} 
 
bar(foo); // prints "bar" then prints "foo" 

However, callbacks are declared inline, at the same point ...

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.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content