Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.29 KB

File metadata and controls

32 lines (24 loc) · 1.29 KB

Callback functions & asynchronous code (Week 2)

This session is about how you in Javascript can assign functions to variables, pass them as arguments, and return them from other functions. You'll learn how to use callback functions to handle asynchronous operations, such as fetching data from a server or responding to user events.

By understanding the difference between synchronous and asynchronous code, you'll be able to write more efficient and responsive programs which will make for a much better user experience.

Contents

Session Learning Goals

By the end of this session, you will be able to:

  • Assign functions to variables
  • Pass functions as arguments
  • Return functions from other functions
  • Call functions inside other functions
  • Distinguish between synchronous and asynchronous code
  • Explain why we need code to run asynchronously sometimes
  • Write and use callback functions
  • Use setTimeout() for asynchronous operations
  • Use addEventListener()
// Example: Using setTimeout() with a callback function
const callback = () => console.log("Do something later");
setTimeout(callback, 1000);
console.log("Do something now");