Рет қаралды 158
In the provided code, there are two asynchronous functions a() and b(), and a function start() which invokes them sequentially. Let's analyze each function:
a():
- It uses await with Promise.reject(1), which immediately rejects the promise with the value 1.
- Since await is used, the rejection is caught by the try...catch block.
- However, Promise.reject(1) immediately throws an error, so the code inside the try block doesn't execute, and it goes directly to the catch block.
- Inside the catch block, it logs the error (1) to the console.
b():
- It returns a promise directly created with Promise.reject(2).
- There is no await in this function, so the rejection is not caught by any try...catch block within the function.
- Therefore, the rejection propagates to the caller of b(). start():
- It sequentially awaits the execution of functions a() and b().
- Now, when you call start(), here's what happens:
start():
- a() is called first, and it logs 1 to the console.
- Then b() is called, which throws an uncaught promise rejection with the value 2.