Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问已解析承诺中的值?_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 如何访问已解析承诺中的值?

Javascript 如何访问已解析承诺中的值?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,从getPeople(0,4)访问值时遇到困难。 函数getPeople(开始、结束){ constpeoplearray=[]; for(让我=开始;我{ Promise.all([getData(),getPeople(0,4)])。然后(item=>{ //console.log(项[0].data.orders)// 设置数据(项目); setPersonData(项目); }); }, []); 项[0]工作正常。 这是当我console.log(项[1])如何访问数据时得到的结果

getPeople(0,4)访问值时遇到困难。

函数getPeople(开始、结束){
constpeoplearray=[];
for(让我=开始;我{
Promise.all([getData(),getPeople(0,4)])。然后(item=>{
//console.log(项[0].data.orders)//
设置数据(项目);
setPersonData(项目);
});
}, []);
项[0]
工作正常。 这是当我
console.log(项[1])
如何访问数据时得到的结果

项[1]
是一系列承诺


函数返回承诺数组

如果您想在
承诺.所有
通话中等待这些承诺,一个选项是:

  useEffect(() => {
    Promise.all([getData(), ...getPeople(0, 4)]).then(item => {
      //console.log(item[0].data.orders);  
      console.log(item[1]); // this will effectively output the people 0 object
      setData(item);
      setPersonData(item);
    });
  }, []);

上述内容将作为
item[0]
接收来自
getData
承诺的解析值(听起来已经是预期值)。然后
item[1]
item[5]
将是您似乎预期的5个人对象。

getPeople
函数返回一个承诺数组

如果您想在
承诺.所有
通话中等待这些承诺,一个选项是:

  useEffect(() => {
    Promise.all([getData(), ...getPeople(0, 4)]).then(item => {
      //console.log(item[0].data.orders);  
      console.log(item[1]); // this will effectively output the people 0 object
      setData(item);
      setPersonData(item);
    });
  }, []);

上面的内容将作为
item[0]
接收来自
getData
承诺的解析值(听起来已经很期待了)。然后
item[1]
item[5]
将是您似乎期待的5个人物对象。

您只需将
getPeople()
返回的数组按如下方式展开:

Promise.all([getData(),…getPeople(0,4)])。然后(item=>{
控制台日志(项目);
});

Promise.all()
需要一个
Promise
数组,您传递的是一个包含另一个数组的数组。

您只需将从
getPeople()
返回的数组展开,如下所示:

Promise.all([getData(),…getPeople(0,4)])。然后(item=>{
控制台日志(项目);
});

Promise.all()
需要一个
Promise
数组,您正在传递一个包含另一个数组的数组。

可以使用useffect中的异步函数使其更可读

//make sure this actually returns the people rather than a load of promises
async function getPeople(start, end) {

    const peopleArray = [];        
    for (let i = start; i <= end; i++) {
      let person = await axios.get(`https://www.hatchways.io/api/assessment/workers/${i}`);
      peopleArray.push(person);
    }
    return peopleArray;

}

//then in useEffect you need to create another async function
useEffect(() => {
    async function getDataAndPersons() {

      const data = await getData();
      setData(data);
      const people = await getPeople(0, 4);
      people.forEach(person => setPersonData(person));

    };
    getDataAndPersons();
}, []);
//确保这实际上是在回报人们,而不是一堆承诺
异步函数getPeople(开始、结束){
constpeoplearray=[];
for(让我=开始;我{
异步函数getDataAndPersons(){
const data=wait getData();
setData(数据);
const-people=wait-getPeople(0,4);
people.forEach(person=>setPersonData(person));
};
getDataAndPersons();
}, []);

使用useffect中的异步函数可以使其更具可读性

//make sure this actually returns the people rather than a load of promises
async function getPeople(start, end) {

    const peopleArray = [];        
    for (let i = start; i <= end; i++) {
      let person = await axios.get(`https://www.hatchways.io/api/assessment/workers/${i}`);
      peopleArray.push(person);
    }
    return peopleArray;

}

//then in useEffect you need to create another async function
useEffect(() => {
    async function getDataAndPersons() {

      const data = await getData();
      setData(data);
      const people = await getPeople(0, 4);
      people.forEach(person => setPersonData(person));

    };
    getDataAndPersons();
}, []);
//确保这实际上是在回报人们,而不是一堆承诺
异步函数getPeople(开始、结束){
constpeoplearray=[];
for(让我=开始;我{
异步函数getDataAndPersons(){
const data=wait getData();
setData(数据);
const-people=wait-getPeople(0,4);
people.forEach(person=>setPersonData(person));
};
getDataAndPersons();
}, []);

这是否回答了您的问题?通过在
异步
函数中使用
然后
等待
。看起来
getPeople
正在返回另一个承诺数组?也许您需要包装另一个
承诺。所有
都围绕
项[1]
…但这正朝着代码气味的方向发展territory@WillCain我在问题中添加了代码。它只是一个数组[getData(),…getPeople(0,4)]这是否回答了您的问题?通过在
async
函数中使用
then
wait
。看起来
getPeople
正在返回另一个承诺数组?也许您需要包装另一个
承诺。所有
围绕
项[1]
…但这正朝着代码气味的方向发展territory@WillCain我在问题中添加了代码。它只是一个数组[getData(),…getPeople(0,4)]