Course contentsShow
← JavaScript
Lesson 1223 of 2,72328. CallbacksFree lesson

Synchronous Callbacks

Array methods like map and forEach use callbacks that execute immediately and synchronously.

Synchronous Callbacks

You'll learn how some callbacks execute immediately and block the call stack until they finish.

The Immediate Execution Pattern

Not all callbacks are asynchronous! Many built-in JavaScript methods accept callbacks that run right now, synchronously, during the current execution. These callbacks don't get queued or scheduled—they execute line-by-line as part of the function that called them.

How Array Methods Use Synchronous Callbacks

Methods like map, forEach, filter, and reduce execute their callback functions immediately for each element:

console.log('Before');

const numbers = [1, 2, 3];
numbers.forEach(function(num) {
  console.log('Processing:', num);
});

console.log('After');

Output:

Before
Processing: 1
Processing: 2
Processing: 3
After

Notice how the callbacks run during the forEach call, not later. The call stack doesn't move past forEach until all three callbacks complete.

Synchronous vs Asynchronous Callbacks

// Synchronous callback
[10, 20].map(function(n) {
  return n * 2;
}); // Returns immediately with [20, 40]

// Asynchronous callback (from earlier lessons)
setTimeout(function() {
  console.log('Later');
}, 0); // Schedules for the task queue

The map callback blocks execution; the setTimeout callback does not.

Why This Matters

Understanding synchronous callbacks helps you predict execution order and avoid blocking the main thread with heavy operations inside array methods.

Key Takeaway: Synchronous callbacks like those in map and forEach execute immediately during the method call, blocking further execution until all iterations complete.