Next: , Previous: , Up: actor-lib A standard library of sorts   [Contents]


6.3 Joiners

The (goblins actor-lib joiners) module provides a means of combining the results of many promises.

The all-of procedure returns a new promise that waits for all of the given promises to resolve before resolving itself. This is very useful when we need to wait for several asynchronous operations to complete successfully before moving on.

To demonstrate this, we’ll use a very useless actor that waits for awhile before returning the same thing it received:

> (define (^procrastinator _bcom delay)
    (lambda (x)
      (sleep delay)
      x))

We can use all-of to wait for all procrastinators to finish procrastinating before writing some output:

> (define a-vat (spawn-vat))
> (define b-vat (spawn-vat))
> (define c-vat (spawn-vat))
> (define p1 (with-vat b-vat (spawn ^procrastinator 1)))
> (define p2 (with-vat c-vat (spawn ^procrastinator 2)))
> (with-vat a-vat
    (on (all-of (<- p1 'foo) (<- p2 'bar))
        (lambda (items)
          (display items)
          (newline))))
; (foo bar)

Note: Currently, all-of calls the resolution procedure with a list as its only argument. In a future release of Goblins we intend to call the resolution procedure with multiple arguments, one for each promise passed to all-of.