我如何将值从.then()块返回给父函数

 getData(link,attribute) {
    const data = {};
    fetch(link)
      .then(response => response.json())
      .then(resData => {
        data[attribute] = resData[attribute];
        return resData;
      })
      .catch(e => e);
    console.log(data[attribute]);
  }

我试图创建一个对象并将resData作为对象的属性插入,然后将该对象返回到函数中,但我一直处于未定义状态。我了解这是由于javascript的异步特性,因此即使在获取responseData之前,也会返回对象的值。我想要实现的是返回resData的最终解析值作为getData函数的返回值,因此当我调用该函数时,我所得到的是解析值,而不必一定将其放置在数据对象中。

Swdream2008 回答:我如何将值从.then()块返回给父函数

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2923569.html

大家都在问