Linear Flow with Async/Await

Linear Flow
πŸ‘¨β€πŸ’Ό Let's refactor a Promise chain to use async/await. This will make the code much more readable!
type Recipe = { id: string; title: string }
type Ingredient = { name: string }

async function loadRecipe(id: string): Promise<Recipe> {
	return { id, title: 'Pancakes' }
}

async function loadIngredients(recipeId: string): Promise<Array<Ingredient>> {
	return [{ name: `${recipeId}-flour` }]
}

async function loadCookingData() {
	const recipe = await loadRecipe('r1')
	const ingredients = await loadIngredients(recipe.id)
	return { recipe, ingredients }
}
🐨 Open
index.ts
and:
  1. Refactor loadUserData to use async/await instead of nested .then()
  2. Keep the same return shape: { user, orders }
  3. Await fetchUser(), then await fetchOrders(user.id)
  4. Export loadUserData
Completion check: await loadUserData() returns Alice (id: '1') and an orders array whose first order has userId: '1'.

Please set the playground first

Loading "Linear Flow with Async/Await"
Loading "Linear Flow with Async/Await"