From 9cc31f6c26b312c2f02bb0e176746753a8f27e76 Mon Sep 17 00:00:00 2001 From: Alexandro castro Date: Tue, 19 Aug 2025 10:45:02 +0100 Subject: [PATCH 1/3] Clarify example in Callback function --- .../en-us/glossary/callback_function/index.md | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/files/en-us/glossary/callback_function/index.md b/files/en-us/glossary/callback_function/index.md index 7f2ea0795ab166d..2327227bfea65f4 100644 --- a/files/en-us/glossary/callback_function/index.md +++ b/files/en-us/glossary/callback_function/index.md @@ -14,16 +14,34 @@ There are two ways in which the callback may be called: _synchronous_ and _async Understanding whether the callback is synchronously or asynchronously called is particularly important when analyzing side effects. Consider the following example: ```js +function doSomething(callback) { + callback(); +} + let value = 1; doSomething(() => { value = 2; }); -console.log(value); +console.log(value); // 2 ``` -If `doSomething` calls the callback synchronously, then the last statement would log `2` because `value = 2` is synchronously executed; otherwise, if the callback is asynchronous, the last statement would log `1` because `value = 2` is only executed after the `console.log` statement. +If `doSomething` calls the callback synchronously, then the last statement would log `2` because `value = 2` is synchronously executed; otherwise, In this asynchronous version, the callback runs later, so when console.log executes, the assignment `value = 2` hasn’t happened yet — 1 is printed. Here is an example of an asynchronous callback: + +```js +function doSomething(callback) { + setTimeout(callback, 0); +} + +let value = 1; + +doSomething(() => { + value = 2; +}); + +console.log(value); // 1 +``` Examples of synchronous callbacks include the callbacks passed to {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.forEach()")}}, etc. Examples of asynchronous callbacks include the callbacks passed to {{domxref("Window.setTimeout", "setTimeout()")}} and {{jsxref("Promise.prototype.then()")}}. From 02404ee79eb0a802c934ce28f841c46e14b3d104 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 19 Aug 2025 14:48:55 -0400 Subject: [PATCH 2/3] Update index.md --- .../en-us/glossary/callback_function/index.md | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/files/en-us/glossary/callback_function/index.md b/files/en-us/glossary/callback_function/index.md index 2327227bfea65f4..012ec03146deb15 100644 --- a/files/en-us/glossary/callback_function/index.md +++ b/files/en-us/glossary/callback_function/index.md @@ -14,37 +14,31 @@ There are two ways in which the callback may be called: _synchronous_ and _async Understanding whether the callback is synchronously or asynchronously called is particularly important when analyzing side effects. Consider the following example: ```js -function doSomething(callback) { - callback(); -} - let value = 1; doSomething(() => { value = 2; }); -console.log(value); // 2 +console.log(value); // 1 or 2? ``` -If `doSomething` calls the callback synchronously, then the last statement would log `2` because `value = 2` is synchronously executed; otherwise, In this asynchronous version, the callback runs later, so when console.log executes, the assignment `value = 2` hasn’t happened yet — 1 is printed. Here is an example of an asynchronous callback: +If `doSomething` calls the callback synchronously, then the last statement would log `2` because `value = 2` is synchronously executed; otherwise, if the callback is asynchronous, the last statement would log `1` because `value = 2` is only executed after the `console.log` statement. + +Examples of synchronous callbacks include the callbacks passed to {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.forEach()")}}, etc. Examples of asynchronous callbacks include the callbacks passed to {{domxref("Window.setTimeout", "setTimeout()")}} and {{jsxref("Promise.prototype.then()")}}. For example, here are example implementations of `doSomething` that call the callback synchronously and asynchronously: ```js +// Synchronous function doSomething(callback) { - setTimeout(callback, 0); + callback(); } -let value = 1; - -doSomething(() => { - value = 2; -}); - -console.log(value); // 1 +// Asynchronous +function doSomething(callback) { + setTimeout(callback, 0); +} ``` -Examples of synchronous callbacks include the callbacks passed to {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.forEach()")}}, etc. Examples of asynchronous callbacks include the callbacks passed to {{domxref("Window.setTimeout", "setTimeout()")}} and {{jsxref("Promise.prototype.then()")}}. - The [Using promises](/en-US/docs/Web/JavaScript/Guide/Using_promises#timing) guide has more information on the timing of asynchronous callbacks. ## See also From 131b5e60c288a426b628e9277eba4718fa627bc3 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 19 Aug 2025 14:49:32 -0400 Subject: [PATCH 3/3] Update index.md --- files/en-us/glossary/callback_function/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/glossary/callback_function/index.md b/files/en-us/glossary/callback_function/index.md index 012ec03146deb15..01f428f302ed0ad 100644 --- a/files/en-us/glossary/callback_function/index.md +++ b/files/en-us/glossary/callback_function/index.md @@ -25,7 +25,7 @@ console.log(value); // 1 or 2? If `doSomething` calls the callback synchronously, then the last statement would log `2` because `value = 2` is synchronously executed; otherwise, if the callback is asynchronous, the last statement would log `1` because `value = 2` is only executed after the `console.log` statement. -Examples of synchronous callbacks include the callbacks passed to {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.forEach()")}}, etc. Examples of asynchronous callbacks include the callbacks passed to {{domxref("Window.setTimeout", "setTimeout()")}} and {{jsxref("Promise.prototype.then()")}}. For example, here are example implementations of `doSomething` that call the callback synchronously and asynchronously: +Examples of synchronous callbacks include the callbacks passed to {{jsxref("Array.prototype.map()")}}, {{jsxref("Array.prototype.forEach()")}}, etc. Examples of asynchronous callbacks include the callbacks passed to {{domxref("Window.setTimeout", "setTimeout()")}} and {{jsxref("Promise.prototype.then()")}}. Here are example implementations of `doSomething` that call the callback synchronously and asynchronously: ```js // Synchronous