为每个 API 调用动态解析 Promise.all 响应并存储数据
Dynamically resolve Promise.all responses for each API call and store data
我现在正在将 3 API 个电话转给 Promise.all。现在,对于每个 API 调用,我需要创建单独的错误处理程序,如果数据正在 return 存储到它自己的对象(对应于相同的 API 对象名称)。
如果我将 test4
传递给 Promise.all,我如何让它生成自己的错误并将数据存储到状态对象,而不是我手动添加这些值?
我试过循环响应,但收到 Object { test3: Promise { "fulfilled" } }
但没有数据。
代码:
import { useCallback, useEffect } from 'react'
const getTest1 = Promise.resolve({ isData: true, isError: false })
const getTest2 = Promise.resolve({ isData: false, isError: true })
const getTest3 = Promise.resolve({ isData: true, isError: false })
export const PromiseAll = () => {
const getInitialData = useCallback(async () => {
try {
const res = await Promise.all([{ test1: getTest1 }, { test2: getTest2 }, { test3: getTest3 }])
for (let i = 0; i < res.length; i++) {
const el = res[i]
console.log(' ~ el', el)
}
const test1 = await res[0].test1
const test2 = await res[1].test2
const test3 = await res[2].test3
test1.isError && console.log('Error in test1', test1.isError)
test2.isError && console.log('Error in test2', test2.isError)
test3.isError && console.log('Error in test3', test3.isError)
const state = {
test1: test1.isData,
test2: test2.isData,
test3: test3.isData,
}
console.log(' ~ state', state)
} catch (error) {
console.log(' ~ Error', error)
}
}, [])
useEffect(() => {
getInitialData()
}, [getInitialData])
return <div>PromiseAll</div>
}
此处示例 console.log Object { test3: Promise { "fulfilled" } }
循环 https://codesandbox.io/s/romantic-mendel-xm5jk9?file=/src/App.tsx
通过使用其名称创建外部对象解决,但仅将 Promise 传递给 Promise.all 这是粗略的原型。
const getInitialData = useCallback(async () => {
try {
const api = [
{ name: 'test1', api: getTest1 },
{ name: 'test2', api: getTest2 },
{ name: 'test3', api: getTest3 },
]
const res = await Promise.all(api.map((el) => el.api))
for (let i = 0; i < res.length; i++) {
const el = res[i]
el.isError && console.log(`Error in ${api[i].name}`, el.isError)
const state = { [api[i].name]: el.isData }
console.log(' ~ state', state)
}
} catch (error) {
console.log(' ~ Error', error)
}
}, [])
Offcourse 我会在 state
上使用一些 prevState 这样以前的数据就不会在每个循环中被覆盖
听起来你想要这样的东西
async function awaitNamedPromises(nameToPromise) {
const namesAndPromises = Object.entries(nameToPromise);
const promises = namesAndPromises.map(([, promise]) => promise);
const results = await Promise.all(promises);
return Object.fromEntries(namesAndPromises.map(([name, promise], index) => [name, results[index]]));
}
const results = await awaitNamedPromises({
test1: Promise.resolve('hello'),
test2: Promise.resolve('world'),
test3: Promise.resolve('bye'),
});
console.log(results);
这会打印出来
{ test1: 'hello', test2: 'world', test3: 'bye' }
我现在正在将 3 API 个电话转给 Promise.all。现在,对于每个 API 调用,我需要创建单独的错误处理程序,如果数据正在 return 存储到它自己的对象(对应于相同的 API 对象名称)。
如果我将 test4
传递给 Promise.all,我如何让它生成自己的错误并将数据存储到状态对象,而不是我手动添加这些值?
我试过循环响应,但收到 Object { test3: Promise { "fulfilled" } }
但没有数据。
代码:
import { useCallback, useEffect } from 'react'
const getTest1 = Promise.resolve({ isData: true, isError: false })
const getTest2 = Promise.resolve({ isData: false, isError: true })
const getTest3 = Promise.resolve({ isData: true, isError: false })
export const PromiseAll = () => {
const getInitialData = useCallback(async () => {
try {
const res = await Promise.all([{ test1: getTest1 }, { test2: getTest2 }, { test3: getTest3 }])
for (let i = 0; i < res.length; i++) {
const el = res[i]
console.log(' ~ el', el)
}
const test1 = await res[0].test1
const test2 = await res[1].test2
const test3 = await res[2].test3
test1.isError && console.log('Error in test1', test1.isError)
test2.isError && console.log('Error in test2', test2.isError)
test3.isError && console.log('Error in test3', test3.isError)
const state = {
test1: test1.isData,
test2: test2.isData,
test3: test3.isData,
}
console.log(' ~ state', state)
} catch (error) {
console.log(' ~ Error', error)
}
}, [])
useEffect(() => {
getInitialData()
}, [getInitialData])
return <div>PromiseAll</div>
}
此处示例 console.log Object { test3: Promise { "fulfilled" } }
循环 https://codesandbox.io/s/romantic-mendel-xm5jk9?file=/src/App.tsx
通过使用其名称创建外部对象解决,但仅将 Promise 传递给 Promise.all 这是粗略的原型。
const getInitialData = useCallback(async () => {
try {
const api = [
{ name: 'test1', api: getTest1 },
{ name: 'test2', api: getTest2 },
{ name: 'test3', api: getTest3 },
]
const res = await Promise.all(api.map((el) => el.api))
for (let i = 0; i < res.length; i++) {
const el = res[i]
el.isError && console.log(`Error in ${api[i].name}`, el.isError)
const state = { [api[i].name]: el.isData }
console.log(' ~ state', state)
}
} catch (error) {
console.log(' ~ Error', error)
}
}, [])
Offcourse 我会在 state
上使用一些 prevState 这样以前的数据就不会在每个循环中被覆盖
听起来你想要这样的东西
async function awaitNamedPromises(nameToPromise) {
const namesAndPromises = Object.entries(nameToPromise);
const promises = namesAndPromises.map(([, promise]) => promise);
const results = await Promise.all(promises);
return Object.fromEntries(namesAndPromises.map(([name, promise], index) => [name, results[index]]));
}
const results = await awaitNamedPromises({
test1: Promise.resolve('hello'),
test2: Promise.resolve('world'),
test3: Promise.resolve('bye'),
});
console.log(results);
这会打印出来
{ test1: 'hello', test2: 'world', test3: 'bye' }