ES6:解析包含其他 Promise 的 Promise,以便父级可以使用 .then
ES6: Resolving Promise containing other Promise so that parent can use .then
我有一个包含另一个 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-->
});
它抛出 ne 错误 Cannot read property 'then' of undefined
getByParamter 方法来自 Class,如
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');
}
});
});
请提出实现我需要的解决方案。
你的箭头函数立即无条件地returns另一个函数,不是承诺!
const getData = () => (dispatch => new Promise(...))
getData()
是函数,所以.then
不存在
自己试试
console.assert(typeof getData() !== "function", "`.then` doesn't exist on a function");
老实说,这段代码应该删除调度回调并让被调用者使用 .then
处理程序,这就是承诺的目的。
const getData = async () => {
const data = await apiService.getByParameter(abc);
return update(data);
});
getData
returns 一个需要调度参数的函数。如果你调用那个函数,你就会得到一个承诺。
const dispatch = useDispatch();
const myPromise = this.getData()(dispatch);
请注意最后一行中的空括号,然后是以 dispatch 作为参数的调用 ()(dispatch)
换句话说,getData
创建了一个可以用来创建承诺的 thunk。
const thunkFunction = getData();
const myPromise = thunkFunction(dispatch);
myPromise.then(...)
我有一个包含另一个 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-->
});
它抛出 ne 错误 Cannot read property 'then' of undefined
getByParamter 方法来自 Class,如
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');
}
});
});
请提出实现我需要的解决方案。
你的箭头函数立即无条件地returns另一个函数,不是承诺!
const getData = () => (dispatch => new Promise(...))
getData()
是函数,所以.then
不存在
自己试试
console.assert(typeof getData() !== "function", "`.then` doesn't exist on a function");
老实说,这段代码应该删除调度回调并让被调用者使用 .then
处理程序,这就是承诺的目的。
const getData = async () => {
const data = await apiService.getByParameter(abc);
return update(data);
});
getData
returns 一个需要调度参数的函数。如果你调用那个函数,你就会得到一个承诺。
const dispatch = useDispatch();
const myPromise = this.getData()(dispatch);
请注意最后一行中的空括号,然后是以 dispatch 作为参数的调用 ()(dispatch)
换句话说,getData
创建了一个可以用来创建承诺的 thunk。
const thunkFunction = getData();
const myPromise = thunkFunction(dispatch);
myPromise.then(...)