Skip to content

Commit 8a7e7f1

Browse files
committed
partial fix: explain-the-concept-of-a-callback-function-in-asynchronous-operations
1 parent 0983882 commit 8a7e7f1

File tree

1 file changed

+5
-7
lines changed
  • questions/explain-the-concept-of-a-callback-function-in-asynchronous-operations

1 file changed

+5
-7
lines changed

questions/explain-the-concept-of-a-callback-function-in-asynchronous-operations/en-US.mdx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A callback function is a function that is passed as an argument to another funct
3232

3333
### Example of a synchronous callback
3434

35-
```js
35+
```js live
3636
function greet(name, callback) {
3737
console.log('Hello ' + name);
3838
callback();
@@ -76,13 +76,11 @@ fetchData((data) => {
7676

7777
When dealing with asynchronous operations, it's important to handle errors properly. A common pattern is to use the first argument of the callback function to pass an error object, if any.
7878

79-
```js
79+
```js live
8080
function fetchData(callback) {
81-
setTimeout(() => {
82-
const error = null;
83-
const data = { name: 'John', age: 30 };
84-
callback(error, data);
85-
}, 1000);
81+
// assume asynchronous operation to fetch data
82+
const { data, error } = { data: { name: 'John', age: 30 }, error: null };
83+
callback(error, data);
8684
}
8785

8886
fetchData((error, data) => {

0 commit comments

Comments
 (0)