I am designing an application that has some scripting capabilities: the host application may call the scripts to perform some operations when some events occur. To keep it easy and newbie-friendly, there is a single thread for everything: this means that if the script calls something blocking, the entire application hangs. So, I either use callbacks or polling to wait for a result of a blocking call to be ready. What is ugly in this approach, is that a function that happens to call a blocking function, has to be split in parts. For polling, I'd have
function do something part 1
blabla
launch something lengthy
schedule do something part 2
end
function do something part 2
if result not ready then schedule do something part 2
use result
blabla
end
Callbacks are only slightly better:
function do something
blabla
launch something lengthy, when done call callback
end
function callback
use result
blabla
end
If one has several blocking calls, and each one depends on the result of the previous, code gets uglier and uglier. What I'm looking for is for some programming language that have some builtin support for this pattern. Something like
function do something
blabla
launch something lengthy
when done: //fictional keyword
use result
blabla
.... //more and more blocking calls
end
Of course if there are better solutions I'm glad to listen.