is it possible to flush data to the screen from in...
# cfml-general
s
is it possible to flush data to the screen from inside an async loop?
a
You might be able to pass the output buffer thingey (possibly not its actual name) from the
PageContext
object into yer async code and append to it? @bdw429s didn't we look at something like this before, a few months ago? Not sure where we got to, and it's 00:30 here (only just noticed this), so I'm going to bed instead of looking further...
b
If you can write output to the main page from an async loop, I would assume you can flush that output. I saw the question, but didn't reply myself because I didn't have time to actually go test it right then, and it seems @Scott Steinbeck could also just test it himself and then come back and tell us what the answer is 🙂
It's also not quite clear just what sort of "async loop" he's talking about. (I can think of several things that he could mean)
At first blush, this approach does NOT work.
Copy code
echo( 'start<br>' )
flush;
[1,2,3,4,5].each( (i)=>{
    sleep( randRange( 1, 5000 ) )
    echo( 'Iteration #i#<br>' )
    flush;
}, true );
When not async, you get the output flushed as it goes, but once you make it async, it seems Lucee waits until the entire each() is done before anything gets output
s
That was my experience as well, but I just assumed I was doing something wrong. My scenario is exactly like your example
b
This version seems to work OK
Copy code
responseStream = getPageContext().getResponseStream();
echo( 'start<br>' )
flush;
[1,2,3,4,5].each( (i)=>{
    sleep( randRange( 1, 5000 ) )
    responseStream.print( 'Iteration #i#<br>' )
    responseStream.flush()
}, true );
The outputs come up in the browser as they happen in a random order
I'd put in a ticket
@Scott Steinbeck
s
I’ll add one in the morning, I’m headed to bed. Thanks for providing a workaround
👍 1