Posted to tcl by apn at Fri Jan 08 05:55:34 GMT 2016view pretty

Example session of checking urls in parallel (*async, non-blocking*) using promises:

% set urls [list]
% set gate [checkurls $urls]
% $gate done puts
{ ok} { error} { ok}
% $gate destroy

And the actual promise-based code:

package require http
package require lambda
proc checkurl {url} {
    set prom [promise::Promise new]
    http::geturl $url -method HEAD -command [lambda {prom tok} {
        upvar #0 $tok url_state
        $prom fulfill [list $url_state(url) $url_state(status)]
        ::http::cleanup $tok
    } $prom]
    return $prom

proc checkurls {urls} {
    return [promise::all [lmap url $urls {checkurl $url}]]