我有一个承诺,其中包含另一个包含解析器的 API 调用者承诺。现在,当我想使用 .then 作为父承诺时,我无法做到这一点,错误说Cannot read property 'then' of undefined,下面是我的示例代码
const getData = () => dispatch => new Promise((resolve) => {
return apiService
.getByParameter(abc)
.then((data) => {
dispatch(update({
name: data.name
}));
resolve();
})
.catch(() => {
});
});
现在每当我尝试做
this.getData().then({
<--something-->
});
它会抛出错误Cannot read property 'then' of undefined
getByParamter 方法来自一个类,如下
getByParameter(...params) {
const endpoint = `${this.getEndpoint.call(this, ...params)}`;
const timeInitiated = performance.now();
return request(() => axios.get(endpoint, extraHeaders), timeInitiated,
endpoint, ACTIONS.ACTION_GET);
}
const request = (rest, timeInitiated, endpoint, action) =>
new Promise((resolve, reject) => {
rest().then(({ data }) => {
const timeResolved = performance.now();
const timeCalculated = millisToMinutesAndSeconds(timeResolved - timeInitiated);
if (endpoint !== LOGS_ENDPOINT && timeCalculated > MAX_EXECUTION_TIME) {
apiLogger.warn(`The endpoint ${endpoint} took ${timeCalculated} seconds for ${action}`);
}
resolve(data);
})
.catch((response) => {
if (!isCancel(response)) {
reject(response);
} else {
apiLogger.debug('Request cancelled');
}
});
});
请建议应该采取什么解决方案来实现我的需要。
叮当猫咪
红颜莎娜
相关分类