parent
1604cb9d8c
commit
ff000c87ed
5 changed files with 140 additions and 20 deletions
@ -0,0 +1,27 @@ |
||||
The difference between Asynchronous and Synchronous code is that Asynchronous code does not block the execution of the program while Synchronous code does. |
||||
|
||||
## Asynchronous code |
||||
|
||||
Asynchronous code is executed in the background and it does not block the execution of the program. It is usually used to perform tasks that take a long time to complete, such as network requests. |
||||
|
||||
```js |
||||
console.log('Before'); |
||||
|
||||
setTimeout(() => { |
||||
console.log('Hello'); |
||||
}, 1000); |
||||
|
||||
console.log('After'); |
||||
``` |
||||
|
||||
## Synchronous code |
||||
|
||||
Synchronous code is executed in sequence and it blocks the execution of the program until it is completed. If a task takes a long time to complete, everything else waits. |
||||
|
||||
```js |
||||
console.log('Before'); |
||||
|
||||
for (let i = 0; i < 1000000000; i++) {} |
||||
|
||||
console.log('After'); |
||||
``` |
@ -0,0 +1,24 @@ |
||||
In order to handle errors in async/await, we can use the `try/catch` statement. |
||||
|
||||
## Rejecting a promise |
||||
|
||||
```js |
||||
const promise = new Promise((resolve, reject) => { |
||||
reject(new Error('Something went wrong')); |
||||
}); |
||||
``` |
||||
|
||||
## Try/catch statement |
||||
|
||||
```js |
||||
async function main() { |
||||
try { |
||||
const result = await promise; |
||||
console.log(result); |
||||
} catch (error) { |
||||
console.log(error.message); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
The `catch` block will be executed when the promise is `rejected` or when an error is thrown inside the `try` block. |
@ -0,0 +1,38 @@ |
||||
In order to handle errors in promises, we can use the `catch` method or the second argument of the `then` method. |
||||
|
||||
## Rejecting a promise |
||||
|
||||
```js |
||||
const promise = new Promise((resolve, reject) => { |
||||
reject(new Error('Something went wrong')); |
||||
}); |
||||
``` |
||||
|
||||
## Catch method |
||||
|
||||
In this method, we can pass a `callback` function that will be called when the promise is `rejected`. |
||||
|
||||
```js |
||||
promise |
||||
.then((result) => { |
||||
console.log(result); |
||||
}) |
||||
.catch((error) => { |
||||
console.log(error.message); |
||||
}); |
||||
``` |
||||
|
||||
## Second argument of the then method |
||||
|
||||
In this method, we can pass two `callback` functions as arguments. The first one will be called when the promise is `resolved` and the second one will be called when the promise is `rejected`. |
||||
|
||||
```js |
||||
promise.then( |
||||
(result) => { |
||||
console.log(result); |
||||
}, |
||||
(error) => { |
||||
console.log(error.message); |
||||
} |
||||
); |
||||
``` |
@ -0,0 +1,14 @@ |
||||
The `finally` block will be executed when the promise is `resolved` or `rejected`. |
||||
|
||||
```js |
||||
promise |
||||
.then((result) => { |
||||
console.log(result); |
||||
}) |
||||
.catch((error) => { |
||||
console.log(error.message); |
||||
}) |
||||
.finally(() => { |
||||
console.log('Finally Promise has settled'); |
||||
}); |
||||
``` |
Loading…
Reference in new issue