Delve into core techniques for handling and troubleshooting asynchronous code using callbacks, promises, and async/await. This quiz enhances your understanding of error handling, data flow, and common pitfalls in asynchronous programming.
In a situation where multiple nested callbacks result in messy and hard-to-follow code, what is this commonly referred to?
Explanation: The correct term for deeply nested callbacks making code difficult to read and maintain is 'Callback Hell.' 'Promise Stack' and 'Async Trap' are not standard terms in asynchronous programming. 'Event Loop Tunnel' also does not describe this phenomenon. Only 'Callback Hell' precisely matches the described problem and is widely recognized.
Consider the following promise: promise.then(doSomething).catch(doError). What happens if doSomething throws an error?
Explanation: If doSomething throws an error inside .then, the promise transitions to a rejected state, and .catch executes doError. 'Execution stops with an uncaught error' is incorrect because the error is caught by .catch. 'doSomething is retried automatically' does not occur; errors are not retried by default. 'The promise is resolved with undefined' is not accurate because an error leads to rejection, not resolution.
Which keyword must be used before a function definition to allow the use of 'await' inside that function?
Explanation: Placing the 'async' keyword before a function enables the use of 'await' within that function. Using 'await' or 'promise' before the function doesn't enable this functionality, and 'defer' has no special meaning in this context. Only 'async' is recognized by the language to provide asynchronous capabilities.
Given 'return fetch(url).then(res =u003E res.json())', what is a common mistake if you forget to return in the arrow function?
Explanation: If you omit the return statement in the arrow function, the resolved value will be undefined because nothing is passed to the next 'then'. 'The promise never settles' is incorrect because the promise still resolves, just with undefined. There is no syntax error, and no infinite loop is created. The missing return specifically results in undefined as the value.
Suppose two asynchronous functions update the same variable without coordination. What kind of bug is most likely to occur?
Explanation: When multiple asynchronous operations update shared state without proper control, a race condition can occur, leading to unpredictable results. 'Time Overflow' and 'Loopback Error' are unrelated to this scenario. 'Promise Leak' refers to unhandled promises, not conflicting data updates. Therefore, 'Race Condition' best describes the likely issue.