【发布时间】:2020-03-10 00:16:27
【问题描述】:
我有以下代码,其中 registerCustomer 函数将调用 registerCustomerApi 从 Rest Api 获取数据。我想添加承诺,以便在返回前端之前等待休息 api 返回响应。请问如何添加promise?
export const registerCustomer = functions.https.onRequest(async (request, response) => {
try {
//Consume api to validate whether is valid customer
var result = registerCustomerApi(request.body.CompanyId, request.body.DateOfBirth, request.body.MobileNo);
if (result != null) {
response.send(result);
}
else {
response.send(request.body);
}
}
catch (error) {
console.error(error);
}
})
function registerCustomerApi(companyId: String, dateOfBirth: String, mobileNo: String) {
try {
request.get(`http://localhost:57580/api/aes_brand/customer/validate_customer/SG01/1990-01-01/8299687`)
.then(function (response) {
return response;
})
.catch(function (err) {
console.error(err);
});
}
catch (error) {
console.error(error);
}
}
【问题讨论】:
-
make ` async function registerCustomerApi()` async 然后 await 应用到
await request.get(localhost:57580/api/aes_brand/customer/validate_customer/SG01/…) -
request支持 Promise 吗?你用的是什么http库?
标签: typescript promise