0

I have created a CFC to process and return comments posted back to the submitting page. It just returns a JSON string of the comment made back to the browser which is then inserted into the DOM using jQuery.

However, I also want to send an email to notify the author that a new comment has been posted. I want to do this AFTER the JSON has been returned to the browser because it will speed up the user interface than having to wait for an email to be sent before updating the DOM.

I am currently testing this and can't see that ColdFusion will execute anything after a <cfreturn> tag. For example the following is not working for me:

<cffunction>
....
<cfreturn NewComment/>
<!--- Anything after the cfreturn above doesn't seem to get executed --->
<cfmail to="somebody@domain.com" from="nobody@domain.com">
A new comment is available for you to read
</cfmail>
</cffuntion>

However this DOES work:

<cffunction>
....
<cfmail to="somebody@domain.com" from="nobody@domain.com">
A new comment is available for you to read
</cfmail>
<cfreturn NewComment/>
</cffuntion>

What's the deal with this and how could I achieve what I'm trying to do? If I wanted to do a whole lot more than just send an email then the DOM would have wait a significant amount of time before being updated which makes the user experience sluggish.

volume one
  • 6,800
  • 13
  • 67
  • 146
  • 6
    The server's response is only sent to the client once the template is completely processed. You can flush the output buffer early using ``, but your approach sounds more like a job for ``. – Alex Oct 01 '16 at 17:02
  • 1
    Alex is correct, you should look into cfthread which allows you to asynchronously execute code on the server while not holding up the original thread. Another option would be to put a record in a database table and have a scheduled task send the actual E-mail a few minutes later. – Brad Wood Oct 01 '16 at 21:52

1 Answers1

5

A return ends processing of the function. Anything after a return is not processed.

Evik James
  • 10,335
  • 18
  • 71
  • 122