Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Promises, Cancelation and AbortController in Deno
Dean Radcliffe
Dean Radcliffe

Posted on

     

Promises, Cancelation and AbortController in Deno

As excited as I am about Deno (the new Node-like runtime), and that it has cancellation semantics, I don't like seeing that they bypassed an over 10 year old concept of Observables, in favor of a barely adopted standard for cancellation - the AbortController.

Should you want to shut down a server, using the oak framework (an anagram of koa, get it?), you have to do the following:

import{Application}from"https://deno.land/x/oak/mod.ts";constapp=newApplication();constcontroller=newAbortController();const{signal}=controller;constlistenPromise=app.listen({port:8000,signal});// In order to close the sever...controller.abort();// Listen will stop listening for requests and the promise will resolve...awaitlistenPromise;

(From:https://github.com/oakserver/oak#closing-the-server)

There are at least two things about this I don't like:

  • Iflisten wasn't called with thesignal, there is no way to shut down the server
  • It's unclear to me whether an aborted server ought to be considered a resolved promise (a value, when awaited), or a rejected Promise.

The problem of needing to know you're setting up a cancelable thing is unfortunate. If it's something you need to know in advance - it's easy to forget. And people won't fall into the 'pit of success' if you impose more upon them to do up front. By the time you know a thing needs to be cancelable, it's often already caused problems.

The problem of Promise cancellation is a deep one, and I'll just leave it at "Promises aren't a good fit for cancellation". That's why the proposal to add cancelation natively to Promisesdied in committee.

Here's what the code would look like if the server were an Observable.

import{Application}from"https://deno.land/x/oak/mod.ts";constapp=newApplication();constcontroller=newAbortController();const{signal}=controller;constlistener=app.listen({port:8000,signal}).subscribe()// In order to close the sever...listener.unsubscribe();// etc..

It clearly fixes those problems. It may introduce a new one IF the cancelation itself is an async operation, and I suspect it may have been because of this, but I don't know whether shutting down a server is a long thing, if you accept that it means canceling running activity.


In short, these days I'm often asking "Why not Observables?" and I would love to hear your answer. Observables are the best proxy I've seen for "A process you start up (possibly in the future), and may want to shut down."

Observables are like having strings around for UNIX processes - they are not executing at the time they're defined, but they begin executing when you callsubscribe(), and the object returned has anunsubscribe() method which sends them aCtrl-C:

In a contrived example:

import{Observable,Subscription}from'rxjs'interfacefindResults{(finder:string):Observable}constresultFinder:string="ls -l magic*"constresults:Observable=findResults(resultFinder)letprocess:SubscriptionconstfindMagicBag=(item)=>{if(item==="magic-bag.md"){console.log("Found my magic bag!");process.unsubscribe();}}// starts executing on this next lineprocess=results.subscribe(findMagicBag);

Again, we don't have to 'add' cancelation, it's just baked in. And it's a good thing, because once we've found the thing we're looking for,why not shut the process down? In servers, particularly, leaving handles open for longer than you need to is problematic.

So I'm excited about Deno, and Oak. And Promises are way better than callbacks. But let's not stop the improvements there, especially in this greenfield area. And with anObservable Proposal in TC39 that couldn't possibly fare worse than Promise cancelation did, we should take a look and see if that fits the bill. :)


Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Chicago, USA
  • Joined

More fromDean Radcliffe

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp