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


6.6 Joiners

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

Procedure: all-of promises …

Return a promise which resolves on resolution of all promises.

As an example, suppose there is an actor which waits for a period of time before returning whatever it received:

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

all-of can wait for all procrastinators to finish 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)
Procedure: all-of* promises

Like all-of, but promises is a list of promises.

Procedure: race promises …

Return a promise which resolves when the first promise in promises settles. If that promise is broken then so is the returned promise.

Suppose we’d like to talk to an actor named slowpoke. However, as the name implies, slowpoke doesn’t always respond in a timely manner. Suppose also that we had another actor named timeout that responds after a specified amount of time has passed. We can use race to wait for slowpoke but give up after awhile and move on:

;; Say "hey" to slowpoke, but only wait a minute for their response.
(race (<- slowpoke 'hey) (<- timeout 60))

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