收益率电话返回承诺

时间:2018-08-24 00:53:06

标签: reactjs react-redux redux-saga saga

所以我正在尝试学习react-redux-saga。我正在构建一个简单的应用程序,该应用程序仅从API抓取用户数据并显示它。这是我的传奇中的生成器函数:

export function* fetchImage() {
      try {
        const response = yield call(fetch, 'https://randomuser.me/api/');
        console.log("Response 111", response);
        const responseBody = response.json();
        yield put(setImage(response));
      } catch (e) {
        yield put(fetchFailed(e));
      }
      return;

    }

它只是对URL进行yield调用,然后调度一个动作。但是,响应对象不会以JSON的形式返回,而是看起来像返回了一些响应对象:

enter image description here

我不确定该对象如何处理。如果您直接通过浏览器访问URL,它将返回一个JSON对象。如何获取返回JSON的信息?

所以我的response.json()返回此对象:

enter image description here

看起来像我想要的对象在[[PromiseValue]]中。那是什么,我如何实现我的价值?

2 个答案:

答案 0 :(得分:1)

解析后,您始终可以访问响应对象的主体:

const response = yield call(fetch, 'https://randomuser.me/api/'); const responseBody = yield response.json(); // HERE is what you want

答案 1 :(得分:0)

通过使用call方法,这是完成相同工作的另一种方式。

const response = yield call(fetch, 'https://randomuser.me/api/');
const body = yield call([response, 'json']);
console.log(body)

另一种方式

const body =  yield call([response, response.json]);
相关问题