it would have been better if you have showed how you are calling it in your script, especially in which route block, but anyways,
1. In transaction-less scenario, after calling this function the script execution will continue, so you have to send some provisional reply, such as "100 Trying" and then exit. Upon receiving http response, named-route will be executed in failure route context and you can send the actual / final sip response e.g. 302.
2. If you have created transaction, then you send provisional reply "100 Trying" BEFORE calling this function, since script execution will be paused upon function call. When a http response is received you will be in named-route but still be in old context (main or branch but with async related limitations) and can send final sip response e.g. 302.
Also note that things will dramatically change if you fork the sip call before calling this function, in which case it is best to call this function in branch route instead of main or failure route.
Hope this helps.