【问题标题】:How to extract data from api function with a promise in react如何从具有反应承诺的api函数中提取数据
【发布时间】:2022-11-25 17:44:24
【问题描述】:

我使用的是 react-redux,在我已经为新页面/编辑页面实现逻辑的 saga 文件中,我需要实现一个 API 来为客户获取一些代码。

const getCodesById = (Id) => get(`${BASE_URL}/${companyId}/codes`);

export function* getTableById(action) {

    const Id = yield select(getCurrentCustomeId);
    getEarningCodesForCompany(companyId).then((response) => {
        console.log(response) //It shows correct array of objects from api  
        return response;
        });
}

console.log(response) 我可以正确地看到数据。 但是,我不知道如何在该函数外部的某个变量中提取该响应,以便能够在函数getTableById 中使用它。

我尝试使用 const request = yield call(getCodesById(Id));,但使用 yield 我的程序崩溃了。

我怎样才能做到这一点,获得回应并在其他地方使用它?

【问题讨论】:

    标签: javascript reactjs react-redux


    【解决方案1】:
    export function* getTableById(action) {
        const Id = yield select(getCurrentCustomeId);
        const codes = yield call(getEarningCodesForCompany, companyId);
        console.log(codes)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多