Promise-returning delay function

C. Scott Ananian ecmascript at cscott.net
Tue Oct 28 12:39:24 PDT 2014


On Tue, Oct 28, 2014 at 2:24 PM, Andrea Giammarchi
<andrea.giammarchi at gmail.com> wrote:
> I really need to **drop that action** ... not to wait for it to fulfill and
> then do something else.

This is orthogonal to the use of Promises, which just discuss control flow.

> Imagine it's not about network, imagine it's "a lift" ... door are closing,
> someone passes in the middle, what do you do now: sound the alert after
> chopping the person that tried to enter in the meanwhile or change floor ...
> after chopping the person anyway ?

You call the `Door.interrupt()` API method to indicate that a person
is present/cancel the opening.

Again, this is orthogonal to the Promise that the `Door.close()`
method returned.  If `Door.close()` returned a Promise, then that
promise could then resolve to "cancelled" value (instead of
"success"), or else you could decide that rejecting the promise with a
`DoorCloseInterrupted` exception makes more sense.

The asynchronous cancellation mechanism is not related to Promises,
and shouldn't be conflated with the Promise API.  The Promise
mechanism just directs control flow after the door has either been
successfully closed or cancelled.

Concretely:
```
function tryToAscend() {
return Door.close().then(function() { return raiseLift(); },
function(e) { return Door.open().delay(10*1000).then(tryToAscend); });
};
```

 --scott


More information about the es-discuss mailing list