Skip to content
advanced

Review: a save-all that returns before saving

This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.

You asked a model to save a batch of items and return the saved records. It produced this, with tests. The tests pass — `save` really is called once per item.

export interface Saved<T> {
  id: number
  value: T
}

export type Save<T> = (value: T) => Promise<Saved<T>>

/**
 * Saves every item in order and returns the saved records.
 */
export async function saveAll<T>(items: T[], save: Save<T>): Promise<Saved<T>[]> {
  const results: Saved<T>[] = []

  items.forEach(async (item) => {
    const saved = await save(item)
    results.push(saved)
  })

  return results
}
review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code