Skip to content

E, episode ten (Promises)

A promise is the result of an eventual send to an object:

[code lang="javascript"] def carVow := makeCar <- ("Mercedes") carVow <- moveTo(2,3) [/code]

“<-” is the eventual send operator.

Now you can start making eventual sends to the promise as if it were the object itself.

The “when-catch” clause is used to wait for the resolution of a promise:

[code lang="javascript"] def temperatureVow := carVow < - getEngineTemperature() when (temperatureVow) -> done(temperature) { println("The temperature of the car engine is: " + temperature) } catch error { println("Could not get engine temperature, error:" + error) } println("execution of the when-catch waits for resolution of the promise,") println("but the program moves on immediately to these printlns") [/code]

We can read the when-catch statement as, “when the promise for a temperature becomes done, and therefore the temperature is locally available, perform the main action block… but if something goes wrong, catch the problem in variable error and perform the problem block”. The when-catch construct waits for the temperature to resolve into an actual (integer) temperature, but only the when-catch construct waits (i.e., the when catch will execute later, out of order, when the promise resolves). The program itself does not wait: rather, it proceeds on, with methodical determination, to the next statement following the when-catch. Inside the when-catch statement, we say that the promise has been resolved. A resolved promise is either fulfilled or broken. If the promise is fulfilled, the main body of the when-catch is activated. If the promise is broken, the catch clause is activated.

References can always resolve to local object (vow references) or be unguaranteed references (receiver references) that must be treated as remote objects.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • Google Buzz
  • HackerNews
  • Posterous
  • Reddit
  • Slashdot
  • Tumblr