gpt4 book ai didi

javascript - Node promise 链 : Local functions and how to pass multiple values (code-review)

转载 作者:搜寻专家 更新时间:2023-10-31 23:53:01 24 4
gpt4 key购买 nike

考虑到以下代码,我有以下问题:

#1这些 bluebird.all 函数返回的值需要在后面的函数中可以访问。从我读过的内容来看,我也可以使用 this-context,而无需事先声明变量。但是我在访问嵌套函数中的这些值时遇到了一些问题。

#2spread 方法是为了使这些参数在后续函数中可访问吗?

#3由于这些函数只在 main 方法中工作,它们应该像这样嵌套吗?

#4我找不到更好的条件 promise 链执行解决方案。如果有的话,我更喜欢一个不那么冗长的解决方案。

#5与 #3 相同,但这次嵌套在嵌套函数中。

function create(contactRequestPayload) {

// #1 Local variables vs this
var
contactRequest = {},
property = {};

return bluebird.all([
new ContactRequestModel(contactRequestPayload).save(),
getPropertyById(contactRequestPayload.propertyId),
sendContactMail()
])
// #2 Correct usage of spread
.spread(function (_contactRequest, _property) {
contactRequest = _contactRequest;
property = _property;
})
.then(processFeedbackEmail)
.then(updateModelState)
.then(returnContactRequest)
.catch(errorHandler);

// #3 Nested functions
function processFeedbackEmail() {
// #4 Conditional code execution
if (!_.get(property, "contact.email.feedback")) {
return bluebird.resolve();
}

return createFeedbackObject()
.then(convertToXml)
.then(sendFeedbackMail)
.catch(function (error) {
logger.warn(error);
});

// #5 Nested functions within a nested function
function createFeedbackObject() {
return contactRequestFeedbackObjectService.from(property, contactRequest);
}

function convertToXml(contactFeedbackObject) {
contactRequest.feedbackJson = contactFeedbackObject;
return objectToXmlService.convert(contactFeedbackObject);
}

function sendFeedbackMail(contactFeedbackXml) {
contactRequest.feedbackXml = contactFeedbackXml;
return contactRequestFeedbackMailService.send(property.contact.email.feedback, contactFeedbackXml);
}
}

function returnContactRequest() {
return contactRequest;
}

function sendContactMail() {
return searchServiceClient.sendContactMail(contactRequestPayload);
}

function getPropertyById(propertyId) {
return getPropertyServiceClient().fetchPropertyById(propertyId);
}

function updateModelState() {
contactRequest.state = 'SENT';
return contactRequest.save();
}
}

最佳答案

Promise 链让我们可以在前向流中像同步代码一样对事物进行排序。我发现内联函数重新获得了重要的上下文。为了使用之前的变量,我使用了很好的旧缩进:

function create(contactRequestPayload) {
return bluebird.all([
new ContactRequestModel(contactRequestPayload).save(),
getPropertyServiceClient().fetchPropertyById(contactRequestPayload.propertyId),
searchServiceClient.sendContactMail(contactRequestPayload)
])
.spread(function processFeedbackEmail(contactRequest, property) {
if (_.get(property, "contact.email.feedback")) {
return contactRequestFeedbackObjectService.from(property, contactRequest)
.then(function convertToXml(feedback) {
contactRequest.feedbackJson = feedback;
return objectToXmlService.convert(feedback);
})
.then(function sendFeedbackMail(xml) {
contactRequest.feedbackXml = xml;
return CRFeedbackMailService.send(property.contact.email.feedback, xml);
})
.catch(function (error) {
logger.warn(error);
});
}
})
.then(function updateModelState() {
contactRequest.state = 'SENT';
return contactRequest.save();
})
.then(function returnContactRequest() {
return contactRequest;
})
.catch(errorHandler);
}

(函数名称当然是可选的,但我保留了它们以供引用。)

对于条件,我将其反转,因为返回 undefined 等于 bluebird.resolve()

有些人可能会争辩说这是一个风格问题,但 ES7 自然而然地得出了这个类比:

async function create(contactRequestPayload) {
var [ contactRequest, property ] = await Promise.all([
new ContactRequestModel(contactRequestPayload).save(),
getPropertyServiceClient().fetchPropertyById(contactRequestPayload.propertyId),
searchServiceClient.sendContactMail(contactRequestPayload)
]);
if (_.get(property, "contact.email.feedback")) {
try {
var feedback = await contactRequestFeedbackObjectService.from(property,
contactRequest);
contactRequest.feedbackJson = feedback;
contactRequest.feedbackXml = await objectToXmlService.convert(feedback);
await CRFeedbackMailService.send(property.contact.email.feedback, xml);
} catch (error) {
logger.warn(error);
}
}
contactRequest.state = 'SENT';
await contactRequest.save();
return contactRequest;
}

关于javascript - Node promise 链 : Local functions and how to pass multiple values (code-review),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com