Consider a situation where you need to execute multiple asynchronous operations in sequence, each dependent on the result of the previous one. How would you implement this with Promises?
- Chaining Promises with .then()
- Promise.sequence()
- Promise.all()
- Async/Await
To execute multiple asynchronous operations in sequence, you would chain promises using .then(). This ensures that each operation is dependent on the result of the previous one. This approach maintains a clear and sequential flow in your asynchronous code.
How does the filter method determine which elements to include in the returned array?
- Based on whether elements pass a specified condition provided by a callback function.
- Returns elements at even indices in the array.
- Sorts the array in ascending order and returns the elements that meet a certain criterion.
- Includes all elements except the ones specified in the callback function.
The filter method creates a new array with all elements that pass a test implemented by the provided function. The callback function determines the condition for inclusion, and only elements for which the function returns true are included in the new array. This is commonly used for selecting a subset of elements based on a specific condition.
Can currying be applied to asynchronous functions in JavaScript? How?
- Yes, by using libraries like Lodash
- No, currying is applicable only to synchronous functions
- Yes, by returning a promise from each curried function
- Yes, by using the async/await syntax in each curried function
Currying can be applied to asynchronous functions by returning a promise from each curried function. This allows composing asynchronous operations in a modular and readable manner.
To ensure all errors are caught in an async function, use try/catch along with ________.
- async/await
- catch/error
- reject/resolve
- throw/catch
When working with asynchronous code in JavaScript, using try/catch blocks along with async/await ensures that errors within the async function are properly caught and handled.
Named exports are useful for ___________ functionality across modules.
- hiding
- sharing
- encapsulating
- extending
Named exports in ES6 allow you to share specific functionalities across modules. It is a way to expose selected parts of a module to be used in other modules, making option b) sharing the correct answer.
How can functional composition benefit the readability and maintainability of JavaScript code?
- Enhances code modularity
- Reduces side effects
- Encourages reuse of functions
- All of the above
Functional composition involves combining small, pure functions to create more complex ones, enhancing modularity, reducing side effects, and promoting code reuse. This leads to improved code readability and maintainability.
When using Promises with AJAX, handling network errors is done through the ______ method of the Fetch API.
- error
- fail
- catch
- reject
When using Promises with AJAX, handling network errors is done through the catch method of the Fetch API. The catch block allows you to handle any errors that occurred during the AJAX request.
Side effects in a function include things like modifying a _________ or logging to the console.
- Variable
- Object
- Array
- State
Side effects can include modifying external state, like changing the value of a variable or an object, which goes against the principles of pure functions.
Using the rest operator in function parameters collects the rest of the arguments into an ________.
- Object
- Array
- Iterable
- String
The rest operator (...) in function parameters is used to collect the rest of the arguments into an array. It allows a function to accept a variable number of arguments.
For a function that calculates a discount on a price, how can default parameters be used to make the function adaptable to different discount strategies?
- Use default parameters to set a default discount strategy, like calculateDiscount(price, discount = 0.1).
- Allow users to pass a discount strategy as a parameter and use default parameters to handle cases where no strategy is provided.
- Utilize default parameters for individual discount components, such as percentage and fixed amount, offering flexibility in discount calculation.
- Set default parameters for both price and discount strategy, allowing users to calculate discounts with a default strategy or a custom one.
Default parameters can enhance the adaptability of a discount calculation function. By setting a default discount strategy, users have the option to either accept the default or provide a custom strategy. This design allows for a default behavior while accommodating different discount strategies based on user preferences or specific use cases.