Dependency Injection if service exists
One possible way to handle this situation is for your Module B to define a default, stub service with the same type signature (ie, Interface) as the one you need).  Use that in the YAML.  Then, in your Module A, you can alter the service to the one that the module provides.

That said, I do agree with the comments that you may be bypassing one of the benefits of DI, and creating a tight coupling between your two modules.  Read the linked article to see when it is appropriate.  Core uses it for the language and workspaces subsystems.