在服务中使用这段生成的并发资料加载器之前,对它进行审查。
并发加载资料并保持输入顺序;任一请求失败时让批次失败,保留原始原因,尽可能停止未完成请求,绝不暴露 API token,并始终关闭客户端。
JavaScript
async function loadProfiles(ids, client, apiToken) {
const results = [];
try {
await Promise.all(
ids.map(async (id) => {
try {
const profile = await client.get(`/profiles/${id}`, { apiToken });
results.push({ ...profile, apiToken });
} catch (error) {
console.log(error.message);
return null;
}
}),
);
return results;
} catch (error) {
throw new Error(`Profile load failed: ${error.message}`);
} finally {
await client.close();
}
}
生成代码仅作示例,不代表任何特定模型