Skip to content

feat: Add Promise-based callServiceAsync wrapper for async/await support#1169

Merged
EzraBrooks merged 3 commits intoRobotWebTools:developfrom
ikwilnaarhuisman:chore/use-promises
Mar 9, 2026
Merged

feat: Add Promise-based callServiceAsync wrapper for async/await support#1169
EzraBrooks merged 3 commits intoRobotWebTools:developfrom
ikwilnaarhuisman:chore/use-promises

Conversation

@ikwilnaarhuisman
Copy link
Copy Markdown
Contributor

Public API Changes
Added callServiceAsync with a promise return. Which allows for cleaner usage.

for example: (response based on default srv implementation from: (https://github.com/ros2/common_interfaces/tree/rolling/std_srvs/srv)

// Originally:
service.callService(
  request,
  (response) => {
    if (response.success) {
      console.log("request successful");
    } else {
      console.error("request failed");
    }
  },
  (failedCallbackString) => {
    console.error("request failed");
  }
);

// Which now may be replaced with:
try {
  const response = await service.callServiceAsync(request);
  
  if (response.success) {
    console.log("request successful");
  } else {
    console.error("request failed");
  }
} catch (error) {
  console.error("request failed");
}

In my opinion this is a lot more readable and a bit more normalized in the "typescript / javascript" world.

@ikwilnaarhuisman
Copy link
Copy Markdown
Contributor Author

added a unit test (or integration test... whatever you'd like to call it) based on the https://docs.ros.org/en/hydro/api/rosapi/html/srv/GetTime.html

Which I think is nice considering we'll probably always be using the rosbridge, right?

@ikwilnaarhuisman
Copy link
Copy Markdown
Contributor Author

ikwilnaarhuisman commented Mar 6, 2026

Shit! Seems that ros1 does not support the getTime service from rosapi, should I make the unit test limited to ROS_VERSION 2. Considering everything's sourced well you can easily get that environment variable. LMK!

Copy link
Copy Markdown
Contributor

@EzraBrooks EzraBrooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great contribution! I had done this as a wrapper of callService in my previous project, having it in the core library will be very useful.

For the test, what about creating a service server in the test code itself and then calling that?

@ikwilnaarhuisman
Copy link
Copy Markdown
Contributor Author

ikwilnaarhuisman commented Mar 6, 2026

This is a great contribution! I had done this as a wrapper of callService in my previous project, having it in the core library will be very useful.

lmao: image

For the test, what about creating a service server in the test code itself and then calling that?

Sure! I'll take a gander at how it's done with the other unit tests and push ASAP.

@ikwilnaarhuisman
Copy link
Copy Markdown
Contributor Author

Hooray

@EzraBrooks EzraBrooks merged commit f0316d3 into RobotWebTools:develop Mar 9, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants