Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
JavaScript - Refactor the way we pass args to function

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using rest parameters and popping off the last one and putting it into an array instead would work.

const sendPushNotification = (...args) => {
  const token = args.pop();
  return sendPushNotifications(...args, [token]);
};

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!