Challenge - 5 Problems
Deno Data Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat does this Deno handler return?
Consider this Deno handler function that fetches JSON data from an API and returns a response. What will be the output when this handler is called?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(async (req) => { const response = await fetch("https://jsonplaceholder.typicode.com/todos/1"); const data = await response.json(); return new Response(JSON.stringify({ title: data.title }), { headers: { "Content-Type": "application/json" }, }); });
Attempts:
2 left
💡 Hint
Look at what part of the fetched data is included in the returned JSON.
✗ Incorrect
The handler fetches the todo item, extracts the title property, and returns only that in the JSON response. So the output JSON contains only the title key with its value.
📝 Syntax
intermediateWhich option causes a syntax error in this Deno fetch handler?
Identify which code snippet will cause a syntax error when used as a Deno HTTP handler fetching data.
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(async (req) => { const response = await fetch("https://api.example.com/data"); const data = await response.json(); return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json" }, }); });
Attempts:
2 left
💡 Hint
Check if all await keywords are used properly with async functions.
✗ Incorrect
Option A misses the await keyword before fetch, so response is a Promise, not the resolved response object. This causes an error when calling response.json().
🔧 Debug
advancedWhy does this Deno handler return an empty response?
This handler is supposed to fetch user data and return it as JSON, but it returns an empty response body. What is the cause?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; serve(async (req) => { const response = await fetch("https://jsonplaceholder.typicode.com/users/1"); response.json(); return new Response(JSON.stringify(response), { headers: { "Content-Type": "application/json" }, }); });
Attempts:
2 left
💡 Hint
Check if the JSON extraction is handled correctly with async/await.
✗ Incorrect
The code calls response.json() but does not await it or assign the result. So the JSON data is never extracted and the response object is stringified instead, resulting in an empty or incorrect body.
❓ state_output
advancedWhat is the value of 'count' after multiple fetches in this Deno handler?
This Deno handler counts how many times it has been called by incrementing a variable. What is the value of 'count' after three requests?
Deno
import { serve } from "https://deno.land/std@0.177.0/http/server.ts"; let count = 0; serve(() => { count += 1; return new Response(`Request count: ${count}`, { headers: { "Content-Type": "text/plain" }, }); });
Attempts:
2 left
💡 Hint
Consider if the variable 'count' persists between requests.
✗ Incorrect
The variable 'count' is declared outside the handler and increments on each request. After three requests, it will be 3.
🧠 Conceptual
expertWhich option best explains why this Deno handler uses 'await' with fetch?
Why is it important to use 'await' when calling fetch inside an async Deno handler?
Attempts:
2 left
💡 Hint
Think about what fetch returns and how async functions handle Promises.
✗ Incorrect
'fetch' returns a Promise that resolves to a Response object. Using 'await' pauses the async function until the Promise resolves, so you get the actual response data to work with.
