JavaScript-重构我们通过ARG运行的方式
我已经实现了以下方法:
async function sendPushNotifications(
title,
body,
data,
badge,
sound = "default",
pushNotificationsTokens
) {
// Generate the messages to be sent
const messages = buildMessages(
title,
body,
data,
badge,
sound,
pushNotificationsTokens
);
// Send the generated messages to the FCM server
const tickets = await sendMessagesToFCMServer(messages);
// Finally, handle all possible errors to avoid app removal in stores
await handleErrorsSendingMessages(tickets, pushNotificationsTokens);
}
我想公开一种更简单的方法,以将推送通知发送到唯一的令牌。
function sendPushNotification(
title,
body,
data,
badge,
sound = "default",
pushNotificationsToken
) {
return sendPushNotifications(
title,
body,
data,
badge,
sound,
[pushNotificationsToken]
);
}
如您所见,这里有某种模式。我们得到参数,然后将它们传递到另一种方法,但将pushnotificationstoken推入数组。
我的问题是:是否可以重构第二个功能sendpushnotification
?我的意思是,有没有专业的外观功能代码可以一行执行?
I have implemented the following method:
async function sendPushNotifications(
title,
body,
data,
badge,
sound = "default",
pushNotificationsTokens
) {
// Generate the messages to be sent
const messages = buildMessages(
title,
body,
data,
badge,
sound,
pushNotificationsTokens
);
// Send the generated messages to the FCM server
const tickets = await sendMessagesToFCMServer(messages);
// Finally, handle all possible errors to avoid app removal in stores
await handleErrorsSendingMessages(tickets, pushNotificationsTokens);
}
And I want to expose a simpler method, for sending a push notification to a unique token.
function sendPushNotification(
title,
body,
data,
badge,
sound = "default",
pushNotificationsToken
) {
return sendPushNotifications(
title,
body,
data,
badge,
sound,
[pushNotificationsToken]
);
}
As you can see, there is some kind of pattern here. We get the params and just pass them to the other method, but pushing the pushNotificationsToken into an array.
My question is: is it possible to refactor the second function sendPushNotification
? I mean, any professional look-alike functional code to do it in one line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用REST参数,然后弹出最后一个并将其放入数组中。
从技术上讲,可以将其全部推入一行(如果需要的话,可以将整个应用程序都放在一行上),但这是不那么可读的。
Using rest parameters and popping off the last one and putting it into an array instead would work.
It'd technically be possible to shove it all into one line (you can put entire applications on a single line if you wanted), but that'd be less readable.