Using __________ at the beginning of an async function can help catch synchronous errors.
- try/catch
- await
- throw
- catch
Placing a try/catch block at the beginning of an async function allows you to catch and handle synchronous errors within the asynchronous context. This is important for comprehensive error handling in asynchronous code.
Currying in JavaScript can help in creating functions that are ________ to specific scenarios.
- General
- Flexible
- Limited
- Specialized
Currying in JavaScript can help in creating functions that are specialized to specific scenarios. Currying is a technique where a function with multiple arguments is transformed into a series of functions, each taking a single argument. This enables creating more specialized functions tailored to specific use cases.
How does 'this' behave within a static method?
- Refers to the instance calling the static method
- Refers to the class itself
- Points to the prototype of the class
- Is undefined
Within a static method, 'this' refers to the class itself, not an instance. This is because static methods are called on the class, not on instances, and they operate on class-level functionality. Therefore, 'this' inside a static method points to the class constructor, allowing access to static properties and methods.
How can you create a custom iterable object using ES6 classes?
- Implement a next method with iterator protocol
- Use the Iterable interface
- Add a getIterator method
- Set the isIterable property to true
In ES6, custom iterable objects are created by implementing the next method with the iterator protocol. This method should return an object with the value and done properties. This allows the object to be iterated using a for...of loop.
What considerations should be made when handling JSON data in the response of a Fetch API call with Promises?
- Parsing JSON should be done using the .json() method.
- Directly using responseText is the preferred method.
- JSON handling is automatic; no considerations needed.
- JSON parsing is not supported in Promises.
When handling JSON data in a Fetch API call, it's crucial to use the .json() method to parse the response properly. This method returns a promise that resolves with the result of parsing the body text as JSON, ensuring proper handling of JSON data.
In what scenarios is it more beneficial to use tagged template literals over regular template literals?
- When dynamic string processing or transformation of the template is required.
- When simple string concatenation suffices for the task.
- Tagged template literals are only used in specific edge cases.
- When you need to create a static string with fixed content.
Tagged template literals provide a powerful mechanism for customizing string output, making them beneficial in scenarios where dynamic content manipulation is necessary, such as in i18n (internationalization) or code generation. Regular template literals are simpler and more suitable for static strings.
How does the yield* keyword differ from yield in a generator function?
- Both are equivalent
- yield* is used for delegating to another generator or iterable
- yield is used for async operations
- yield* is used for synchronous operations
The yield* keyword in a generator function is used for delegating to another generator or iterable. It allows you to compose generators and is often used for iterating over iterable objects within the generator function. On the other hand, yield is used to pause the generator and yield a value to the caller.
When an async function throws an error and it is not caught, it results in a rejected __________.
- Promise
- Error
- Exception
- Rejection
When an async function throws an unhandled error, it results in a rejected Promise. The rejection carries the error information, and if not caught using a catch block or try...catch, it can lead to unhandled promise rejections. Understanding this behavior is essential for effective error handling in asynchronous code. Properly handling promise rejections ensures that errors are appropriately addressed, preventing unintended consequences in the application.
When using fetch to make an HTTP request, how do you ensure that HTTP errors are caught?
- response.ok
- response.status
- catch
- handleError
To catch HTTP errors when using the fetch API, you can use the catch method. This method is chained to the Promise returned by fetch and allows you to handle network errors, as well as HTTP errors (e.g., 404 or 500). By providing a callback function to catch, you can manage and respond to errors that may occur during the request.
When iterating over a Map object with a for...of loop, each iteration returns an array where the first element is the _______ and the second is the value.
- key
- index
- property
- length
In a Map object, the first element of the array returned during iteration represents the key, and the second element represents the corresponding value.