强制异步调用以同步方式运行
Force an Asynchronous call to behave Synchronously
在我的 React 应用程序中,我试图根据其他三个值计算一个值。我已将所有计算逻辑包含到后端,这是我对其进行异步调用的微服务。我在其中异步尝试获取该计算值的函数位于许多同步挂钩的中间。
在 UI 层中,我调用了我想要 return 最终结果的函数(return 异步编辑)。那个被调用的函数调用另一个函数,另一个函数调用另一个函数,return 是一个新的 Promise。请参阅下面的代码:
// DateUI.js (layer 1)
selectDate(dateField, flight, idx, saved, momentTime, e) {
if (moment(momentTime).isValid()) {
if (dateField == "StartDate") {
// The initial problematic function call, need to set endDate before I continue on
let endDate = PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix);
flight.set("EndDate", endDate);
}
this.theNextSyncFunction(..., ..., ...);
}
}
// DateActions.js (layer 2)
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then(function(response) {
// response is JSON: {EndDate: "12/05/2016"}
response.EndDate;
}, function(error) {
log.debug("There was an error calculating the End Date.");
});
}
// DateClass.js (layer 3)
getFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let path = '/path/to/microservice';
return this.callServer(path, 'GET', {periodTypeId: periodTypeId, numberOfPeriods: numberOfPeriods, startDate: startDate});
}
// ServerLayer.js (layer 4)
callServer(path, method = "GET", query = {}, data, inject) {
return new Promise((resolve, reject) => {
super.callServer(uri.toString(),method,data,inject).then((data) => {
resolve(data);
}).catch((data) => {
if (data.status === 401) {
AppActions.doRefresh();
}
reject(data);
});
});
}
我的印象是,因为 ServerLayer.js(第 4 层)return 是一个 new Promise
(因此 DateClass.js(第 3 层)),调用 plan.getFlightEndDate(...).then(function(response) {...
在响应返回解决或拒绝之前不会完成。目前还没有发生这种情况,因为 DateUI.js(第 1 层)中的代码将继续调用 this.theNextSyncFunction
,然后在大约 50 毫秒后使用正确的数据解析。
在我继续使用 selectDate() 之前,如何强制 DateUI.js(第 1 层)中的 PlanLineActions.calculateFlightEndDate(...) 完成响应?
我认为您不了解 promise 的工作原理。
首先,函数总是return立即所以你永远不会阻止下一行代码的执行(flight.set
和theNextSyncFunction()
在你的情况下)。这就是 return 承诺的要点:您 立即 得到一个承诺,您可以将回调附加到(使用 then()
),它将被调用 后来。如果你想让代码等待承诺解决你 必须把它放在 then()
回调中。
其次,您的 calculateFlightEndDate()
根本 return 什么都没有 所以 endDate = calculateFlightEndDate()
只是将 endDate
设置为 undefined
。
解决方案
您应该 return 来自 calculateFlightEndDate()
的承诺,然后将您要在之后执行的代码放入 then()
回调中:
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
return plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then((response) => {
// response is JSON: {EndDate: "12/05/2016"}
return response.EndDate;
}, (error) => {
log.debug("There was an error calculating the End Date.");
});
}
if (moment(momentTime).isValid()) {
if (dateField == "StartDate") {
PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix).then((endDate) => {
flight.set("EndDate", endDate);
this.theNextSyncFunction(...);
});
}
}
您还可以考虑使用 ES7 async
and await
,它允许您 编写 您的异步代码,使其 看起来 同步,但在幕后使用承诺来完成同样的事情。
如果某些东西像 ajax 调用一样在事件循环之外,您不能强制它同步。您将需要如下所示的内容:
PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix)
.then(endDate => {
this.theNextSyncFunction(..., ..., ...);
})
为了做到这一点,calculateFlightEndDate
还需要 return 一个承诺,因此承诺是可链接的是一件好事。
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
// return promise!
return plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then(response => {
return response.EndDate; // must return here
}, error => {
log.debug("There was an error calculating the End Date.");
});
}
这应该可以做到。还有一件事:您在服务器调用中加倍承诺。如果某个东西有 .then
,它已经是一个承诺,所以你可以直接 return。无需包含 new Promise
(承诺中的承诺.. 不需要!)
callServer(path, method = "GET", query = {}, data, inject) {
// just return!
return super.callServer(uri.toString(),method,data,inject).then((data) => {
return data;
}).catch((data) => {
if (data.status === 401) {
AppActions.doRefresh();
}
throw data; // throw instead of reject
});
}
在我的 React 应用程序中,我试图根据其他三个值计算一个值。我已将所有计算逻辑包含到后端,这是我对其进行异步调用的微服务。我在其中异步尝试获取该计算值的函数位于许多同步挂钩的中间。
在 UI 层中,我调用了我想要 return 最终结果的函数(return 异步编辑)。那个被调用的函数调用另一个函数,另一个函数调用另一个函数,return 是一个新的 Promise。请参阅下面的代码:
// DateUI.js (layer 1)
selectDate(dateField, flight, idx, saved, momentTime, e) {
if (moment(momentTime).isValid()) {
if (dateField == "StartDate") {
// The initial problematic function call, need to set endDate before I continue on
let endDate = PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix);
flight.set("EndDate", endDate);
}
this.theNextSyncFunction(..., ..., ...);
}
}
// DateActions.js (layer 2)
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then(function(response) {
// response is JSON: {EndDate: "12/05/2016"}
response.EndDate;
}, function(error) {
log.debug("There was an error calculating the End Date.");
});
}
// DateClass.js (layer 3)
getFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let path = '/path/to/microservice';
return this.callServer(path, 'GET', {periodTypeId: periodTypeId, numberOfPeriods: numberOfPeriods, startDate: startDate});
}
// ServerLayer.js (layer 4)
callServer(path, method = "GET", query = {}, data, inject) {
return new Promise((resolve, reject) => {
super.callServer(uri.toString(),method,data,inject).then((data) => {
resolve(data);
}).catch((data) => {
if (data.status === 401) {
AppActions.doRefresh();
}
reject(data);
});
});
}
我的印象是,因为 ServerLayer.js(第 4 层)return 是一个 new Promise
(因此 DateClass.js(第 3 层)),调用 plan.getFlightEndDate(...).then(function(response) {...
在响应返回解决或拒绝之前不会完成。目前还没有发生这种情况,因为 DateUI.js(第 1 层)中的代码将继续调用 this.theNextSyncFunction
,然后在大约 50 毫秒后使用正确的数据解析。
在我继续使用 selectDate() 之前,如何强制 DateUI.js(第 1 层)中的 PlanLineActions.calculateFlightEndDate(...) 完成响应?
我认为您不了解 promise 的工作原理。
首先,函数总是return立即所以你永远不会阻止下一行代码的执行(flight.set
和theNextSyncFunction()
在你的情况下)。这就是 return 承诺的要点:您 立即 得到一个承诺,您可以将回调附加到(使用 then()
),它将被调用 后来。如果你想让代码等待承诺解决你 必须把它放在 then()
回调中。
其次,您的 calculateFlightEndDate()
根本 return 什么都没有 所以 endDate = calculateFlightEndDate()
只是将 endDate
设置为 undefined
。
解决方案
您应该 return 来自 calculateFlightEndDate()
的承诺,然后将您要在之后执行的代码放入 then()
回调中:
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
return plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then((response) => {
// response is JSON: {EndDate: "12/05/2016"}
return response.EndDate;
}, (error) => {
log.debug("There was an error calculating the End Date.");
});
}
if (moment(momentTime).isValid()) {
if (dateField == "StartDate") {
PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix).then((endDate) => {
flight.set("EndDate", endDate);
this.theNextSyncFunction(...);
});
}
}
您还可以考虑使用 ES7 async
and await
,它允许您 编写 您的异步代码,使其 看起来 同步,但在幕后使用承诺来完成同样的事情。
如果某些东西像 ajax 调用一样在事件循环之外,您不能强制它同步。您将需要如下所示的内容:
PlanLineActions.calculateFlightEndDate(periodTypeId, numberOfPeriods, momentTimeUnix)
.then(endDate => {
this.theNextSyncFunction(..., ..., ...);
})
为了做到这一点,calculateFlightEndDate
还需要 return 一个承诺,因此承诺是可链接的是一件好事。
calculateFlightEndDate(periodTypeId, numberOfPeriods, startDate) {
let plan = new Plan();
// return promise!
return plan.getFlightEndDate(periodTypeId, numberOfPeriods, startDate).then(response => {
return response.EndDate; // must return here
}, error => {
log.debug("There was an error calculating the End Date.");
});
}
这应该可以做到。还有一件事:您在服务器调用中加倍承诺。如果某个东西有 .then
,它已经是一个承诺,所以你可以直接 return。无需包含 new Promise
(承诺中的承诺.. 不需要!)
callServer(path, method = "GET", query = {}, data, inject) {
// just return!
return super.callServer(uri.toString(),method,data,inject).then((data) => {
return data;
}).catch((data) => {
if (data.status === 401) {
AppActions.doRefresh();
}
throw data; // throw instead of reject
});
}