Asynchronous Programming in Deno: Promises and Async/Await Quiz Quiz

Explore the essentials of asynchronous programming in Deno using Promises and the async/await syntax. This quiz covers key concepts, syntax, and best practices to help you solidify your understanding of non-blocking code execution in Deno.

  1. Definition of a Promise

    What is a Promise in asynchronous programming as used in Deno?

    1. A synchronous loop that waits for results
    2. A method for declaring global variables asynchronously
    3. A function that executes immediately and returns no value
    4. A way to represent a value that may be available now, later, or never

    Explanation: A Promise is an object representing the eventual completion or failure of an asynchronous operation. It is not a function that runs immediately; that is incorrect. Promises are not used for global variable declaration, nor do they provide synchronous looping behavior. The correct definition is representing a possibly future value.

  2. Purpose of async Functions

    What is the main function of the 'async' keyword when used before a function declaration in Deno?

    1. To shorten the runtime of the function
    2. To automatically run the function synchronously
    3. To guarantee exception handling occurs
    4. To allow the use of 'await' inside the function

    Explanation: Placing 'async' before a function enables the use of 'await' within its body, allowing asynchronous code to be written in a synchronous-like manner. 'Async' does not inherently shorten runtime, nor does it automatically handle exceptions, though exceptions can be caught with try/catch. The function is not run synchronously by simply marking it 'async'.

  3. State of a Newly Created Promise

    When a Promise is newly created in Deno, what is its initial state?

    1. Fulfilled
    2. Rejected
    3. Pending
    4. Complete

    Explanation: A new Promise starts in the 'pending' state, waiting for completion or failure. It only becomes 'fulfilled' when successfully resolved, or 'rejected' on failure. 'Complete' is not a valid Promise state in this context. Fulfilled and rejected are possible later states, but not the initial one.

  4. Using Await Outside Async Functions

    What happens if you use the 'await' keyword outside of an async function in Deno?

    1. It automatically converts the containing function to async
    2. A syntax error will occur
    3. The code will ignore the 'await' and proceed
    4. The program will execute normally and synchronously

    Explanation: Using 'await' outside of an async function leads to a syntax error because 'await' is only valid within async function bodies. The code does not ignore 'await', nor does it execute synchronously. The environment also does not auto-convert functions to async; the programmer must explicitly declare them.

  5. Handling Promise Resolution

    If you want to execute some code after a Promise resolves in Deno, which method should you use?

    1. onResolve()
    2. after()
    3. callback()
    4. then()

    Explanation: The 'then()' method lets you specify what to do after a Promise is resolved. Methods like 'onResolve()', 'after()', and 'callback()' are not standard, and do not exist for handling Promises. Only 'then()' provides this built-in functionality.

  6. Rejection Handling with Promises

    Which method is used to handle errors when a Promise is rejected in Deno?

    1. catch()
    2. finalize()
    3. recover()
    4. fail()

    Explanation: The 'catch()' method is standard for handling errors when a Promise is rejected. There are no built-in methods called 'finalize()', 'fail()', or 'recover()' for this purpose in the language. Only 'catch()' safely deals with Promise rejections.

  7. Promise.all Usage

    Which statement describes the use of Promise.all when handling multiple Promises in Deno?

    1. It only accepts a single Promise as input
    2. It immediately rejects if any promise is pending
    3. It waits for all promises to resolve and returns an array of results
    4. It runs only the first promise and skips the rest

    Explanation: Promise.all allows multiple Promises to run concurrently, waiting for all to resolve before returning an array of their results. It does not reject on pending Promises, skip others, or accept only one Promise. The method handles an iterable of Promises for batch processing.

  8. Returning Value from Async Function

    What does an async function called in Deno always return?

    1. A Number
    2. A Promise
    3. An Array
    4. A Function

    Explanation: Calling an async function always produces a Promise, not a direct Array, Function, or Number. The value you return within the async function becomes the resolution value of the returned Promise. Arrays and numbers can be wrapped as results, but the return type itself is a Promise.

  9. Purpose of Await

    What is the main purpose of using the 'await' keyword within an async function in Deno?

    1. To cancel the Promise immediately
    2. To execute multiple Promises in parallel
    3. To wrap synchronous functions as Promises
    4. To pause execution until the Promise settles and return its result

    Explanation: 'Await' pauses the function execution until the Promise resolves or rejects, returning the result directly. It does not cancel Promises, nor is it designed to process multiple Promises in parallel. While it can await synchronous results, its main role is not to wrap such functions.

  10. Promise Chaining Example

    Given the code example: fetchData().then(process).then(display), what is this pattern called in Deno's asynchronous programming?

    1. Promise scattering
    2. Promise chaining
    3. Promise detaching
    4. Promise recursion

    Explanation: This pattern is known as Promise chaining, where multiple then() methods are linked for sequential execution. 'Promise recursion' is not correct since there is no recursion involved. 'Promise scattering' and 'Promise detaching' are not real terms in this context. Chaining allows actions to happen in order as each Promise resolves.