javascript - How to merge/call the results of 2 callback function into a new function javascript?
问题描述
What I currently have is 2 functions with callbacks.
At the end of validateEvent, there will be 2 variables isDeactivated
and eventOrganizer
.
While at the end of validateCheckIn, there will be 1 variable which is isCheckIn
.
What I want to achieve now is merging the results of the 2 functions validateEvent and validateCheckIn into 1 new function where I can interact with 3 of the variables isDeactivated
, eventOrganizer
and isCheckIn
in it.
What I've found so far is something like this: link
But what if I wanted to add extra code in the newly merged function?
var isDeactivated = false;
var eventOrganizer;
var isCheckIn = false;
function validateEvent(result) {
$.post(
"**displayEventAPI**",
{
id: eventID,
}).done(function (data) {
result(
data["eventList"][0]["event_status"],
data["eventList"][0]["event_creator"]
);
}
)
}
validateEvent(function(event_status, event_creator) {
if (event_status == 0) {
isDeactivated = true;
}
eventOrganizer = event_creator;
console.log(isDeactivated, '--isDeactivated');
console.log(eventOrganizer, '--eventOrganizer');
});
function validateCheckIn(result) {
$.post(
"**displayAttendanceAPI",
{
event_id: eventID,
}).done(function (data) {
for (var i = 0; i < data.attendanceList.length; i++) {
if (data.attendanceList[i].badge_id === badgeID) {
isCheckIn = true;
}
}
result(
isCheckIn
);
}
)
}
validateCheckIn(function(isCheckIn) {
console.log(isCheckIn, '--isCheckIn');
});
解决方案
Try using Promise and async functions for the implementation.
Also wrap your async
function calling inside an another async
function. Because await
keyword can only be used inside an async
function.
var isDeactivated = false;
var eventOrganizer;
var isCheckIn = false;
async function validateEvent() {
return new Promise((resolve, reject) => {
$.post(
"**displayEventAPI**",
{
id: eventID,
}).done(function (data) {
resolve({
event_status: data["eventList"][0]["event_status"],
event_creator: data["eventList"][0]["event_creator"]
});
}
)
})
}
async function validateCheckIn(result) {
return new Promise((resolve, reject) => {
$.post("**displayAttendanceAPI", {
event_id: eventID,
}).done(function (data) {
for (var i = 0; i < data.attendanceList.length; i++) {
if (data.attendanceList[i].badge_id === badgeID) {
isCheckIn = true;
}
}
resolve(isCheckIn);
})
})
}
function thirdFunction(event_status, event_creator, isCheckIn) {
console.log(event_status, '--isCheckIn');
console.log(event_creator, '--isCheckIn');
console.log(isCheckIn, '--isCheckIn');
}
async function execute() {
const { event_status, event_creator } = await validateEvent();
const isCheckIn = await validateCheckIn();
thirdFunction(event_status, event_creator, isCheckIn);
}
execute();
推荐阅读
- ruby-on-rails - 具有多个不同ID的表单,如何保存
- excel - 制作列表重置子例程不删除总计行或其包含的公式
- windows - 如何在命令提示符下安装 PolyML
- url-rewriting - IIS 7 URL 重写规则帮助
- artificial-intelligence - 转置表如何与 Hypermax 一起使用?
- r - 我正在尝试从目录中的许多 pdf 中提取关键字和计数
- workflow - 如何跳过失败的 Rundeck 作业?
- google-api - 如何对 YouTube Analytics API 进行 HTTP 调用?
- java - 批处理:使用目录中类型的第一个文件作为变量
- python - TF2.0中如何将tf.data.Dataset类型的数据切片到一定长度?