Which method is used to catch errors in a Promise?
- catchError
- errorHandler
- catch
- handleError
The correct method to catch errors in a Promise is the catch method. It is used to handle both synchronous and asynchronous errors that may occur during the execution of the Promise. By chaining the catch method to a Promise, you can specify a callback function to handle the errors and take appropriate actions.
How does the ES6 specification handle tail call optimization and what are its limitations in JavaScript implementations?
- ES6 specification doesn't explicitly mandate tail call optimization.
- It mandates tail call optimization, but its implementation varies.
- Tail call optimization is not supported in ES6.
- It is only applicable to specific cases in ES6.
In ES6, tail call optimization is mandated, but its implementation may vary across JavaScript engines. The limitation lies in inconsistent support across different environments, making it crucial to check engine compatibility when relying on this optimization.
Can destructuring assignment be used with arrays, and if so, how does it differ from object destructuring?
- Yes, and it works the same as with objects
- No, destructuring can only be used with objects
- Yes, but the syntax is different from object destructuring
- Yes, and it uses square brackets for the pattern
Destructuring assignment can be used with arrays in JavaScript. However, the syntax differs from object destructuring. With arrays, square brackets are used to indicate the pattern, making it distinct from object destructuring that uses curly braces.
What is the difference in execution timing between callbacks and Promises?
- Callbacks may lead to callback hell due to nested structures, affecting the execution sequence.
- Promises execute asynchronously, allowing better control over the flow of the program.
- Execution timing is the same for both callbacks and Promises.
- Callbacks always execute before Promises.
Promises provide a more straightforward approach to asynchronous programming, allowing developers to handle execution timing more efficiently. Callbacks, especially when nested, can lead to callback hell, making it challenging to manage the sequence of operations. This understanding is crucial for developers aiming to enhance code readability and manage asynchronous tasks effectively using Promises.
If a class extends another class, the constructor of the child class must call _______ to access the parent's properties.
- super()
- parent()
- this()
- extend()
If a class extends another class, the constructor of the child class must call super() to access the parent's properties. The super() function is used to invoke the constructor of the parent class, ensuring that the parent's properties are initialized correctly.
Which ES6 feature can be particularly useful in writing more readable recursive functions?
- Arrow functions
- Destructuring assignment
- Rest and spread operators
- Default function parameters
Arrow functions have a concise syntax, making the code more readable. They are particularly useful in recursive functions where brevity and clarity are crucial.
What is the default behavior of module resolution in ES6 when importing a module without specifying a file extension?
- Resolves to the module with the specified name
- Resolves to the module with the specified name, followed by ".js"
- Resolves to the module with the specified name, followed by ".mjs"
- Resolves to the module with the specified name, followed by ".json"
In ES6, when a module is imported without a file extension, the module resolution algorithm looks for the module with the specified name followed by ".js". If not found, it looks for the module with ".mjs" and then ".json". This flexibility allows developers to omit file extensions, and the resolution algorithm handles it accordingly.
An async function always returns a __________.
- Promise
- Callback
- Synchronous Value
- Undefined
In an asynchronous function, the return value is always a Promise. This allows for better handling and chaining of asynchronous operations.
Question 3: When setting up a mono-repo with multiple packages, how does ES6 module resolution impact the sharing of code across different packages?
- Facilitates code sharing through relative imports
- Hinders code sharing across packages
- Enables code sharing through global imports
- Requires a centralized package for sharing
In a mono-repo with multiple packages, using relative imports in ES6 modules can facilitate code sharing. It allows packages to import modules from one another directly, enhancing modularity and reusability. This approach avoids a centralized package and promotes a more decentralized and modular code organization.
In functional programming, _________ is a technique used to avoid side effects by ensuring data is not modified.
- Mutable State
- Immutability
- Asynchronous
- Prototyping
In functional programming, immutability is a technique that involves not modifying the data once it is created. This helps avoid side effects and makes functions more predictable and easier to reason about. Immutability ensures that data remains constant throughout its lifecycle.