Asynchronous Programming with Promises and Async/Await Quiz Quiz

Challenge your understanding of asynchronous programming by exploring how Promises and async/await work, their syntaxes, error handling techniques, and sequencing of tasks. This quiz is crafted for developers aiming to deepen their practical knowledge in managing asynchronous functions using modern syntax.

  1. Identifying Promise States

    Which of the following is NOT a valid state of a Promise in JavaScript?

    1. pending
    2. completed
    3. fulfilled
    4. rejected

    Explanation: The valid states of a Promise are pending, fulfilled, and rejected. The option 'completed' is not an official state, although it may sound correct as a general term. 'Pending' describes a Promise that is not yet resolved or rejected, 'fulfilled' is when the Promise is successfully resolved, and 'rejected' is when it fails. Only 'completed' does not fit into the defined Promise states.

  2. Handling Promise Results

    What is the main purpose of the .then() method in working with Promises, such as 'fetchData().then(handleSuccess);'?

    1. It schedules a task to run after the Promise is fulfilled
    2. It synchronously pauses the code until the result is available
    3. It cancels the Promise if it is taking too long
    4. It catches errors during the Promise execution

    Explanation: The .then() method is used to schedule execution of a callback when a Promise is fulfilled, allowing access to its resolved value. It does not catch errors (that is what .catch() is for), nor does it pause code execution like the incorrect 'synchronously pauses' option suggests. Promises cannot be cancelled directly using .then(), so the last option is also incorrect.

  3. Async Function Return Values

    What type of value does an async function return by default, such as with 'async function myFunc() { return 3; }'?

    1. An undefined value
    2. A regular number
    3. A rejected Promise
    4. A resolved Promise

    Explanation: An async function always returns a resolved Promise with the value provided in the return statement. That means in the example, it returns a Promise that resolves with 3, not just the number 3. The first option, rejected Promise, is wrong unless an exception is thrown. 'Undefined value' is only correct if no return statement is present, and a regular number is not directly returned from async functions.

  4. Error Handling in Async/Await

    When using async/await, which structure is commonly used for handling errors in asynchronous operations?

    1. for/loop
    2. switch/case
    3. map/filter
    4. try/catch

    Explanation: A try/catch block is commonly used to handle errors inside async functions, especially when using await. The other options—switch/case, for/loop, and map/filter—are not error handling constructs but are used for flow control and data manipulation tasks. Only try/catch directly relates to capturing and managing asynchronous exceptions.

  5. Comparing .then() and await

    Which statement best describes the main difference between using .then() and the await keyword in asynchronous programming?

    1. await can only be used outside of functions while .then() can be used anywhere
    2. .then() makes code run faster than await
    3. await provides a more readable, synchronous-like syntax in async functions, while .then() uses callbacks
    4. .then() allows synchronous execution, while await forces asynchronous execution

    Explanation: The await keyword makes asynchronous code appear more readable and 'synchronous' when used inside async functions, while .then() relies on callbacks to handle resolved Promises. Contrary to some distractors, await cannot be used outside async functions and neither construct guarantees faster execution; both are asynchronous. .then() does not execute synchronously, and neither ensures faster runtime.