After creating a user through the management API, how long does it take. This code fails to see the new user but the same command works if I wait for some time
The user search feature you are using is not synchronous to change operations performed on a user as demonstrated by the flow you described in your question. In addition, there’s also not a concrete amount of time you need to wait as in ideal conditions is almost as if it was synchronous, but in conditions where there may be additional load in the system or the user profile data of the associated user is also complex, the window of time might increase.
I’m assuming that the code provided was just to illustrate the issue and it’s just sample code, otherwise for this specific case the recommendation would be to not depend on searching for the new user and instead just reuse the information available from the create call. If you use cases where this asynchronous nature is completely blocking from implementing your goal it would also be interesting if you could share them.