MST

星途 面试题库

面试题:TypeScript 异步函数中剩余参数的基础应用

编写一个异步函数 `fetchData`,它接受任意数量的 URL 作为剩余参数,函数需要并行地获取这些 URL 的数据(可以假设使用 `fetch` 模拟获取数据),最后返回所有获取到的数据组成的数组。请使用 TypeScript 编写,并明确函数参数和返回值的类型。
39.9万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
async function fetchData(...urls: string[]): Promise<any[]> {
    const promises = urls.map(url => fetch(url).then(response => response.json()));
    return Promise.all(promises);
}