question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

:bird: Feature (coroutines): Support yielding autoDisposing disposers (like Go defer)

See original GitHub issue

Since we don’t have using blocks in JavaScript, we don’t currently have a good way to use disposers in generators.

Proposal: add support for yielding AutoDisposers:

Assuming getConnection returns a bluebird disposer:

Promise.coroutine(function* () {
  var connection = yield getConnection(options).autoDispose();
  // connection is usable here, and will dispose after coroutine completes
});

This would act like Go’s defer:

func DoSomething() err error {
    connection, err := db.Connect(options)
    if err != nil {
        return
    }
    defer connection.Close()
    // connection is usable here, and will close after function returns automatically.
}

Some thoughts:

  • Its nice for autoDispose to be explicit, otherwise its not clear that you can’t save the connection elsewhere and use it later

  • This is “safer” than Go’s defer, because you can forget to add defer conn.Close() in Go and the connection will stay open 😃

  • On the other hand defer connection.Close() makes it more clear what happens, not sure if autoDispose() is that clear? Defer is also more generic - you could schedule any sort of cleanup with it. If Promise.coroutine was imported like so:

    var co = require('bluebird').coroutine;
    
    SomeClass.prototype.someMethod = co(function* () {
     this.context.push(item);
     yield co.defer(_ => this.context.pop(item))
     // now you can do things with a `this.context` that contains the item 
     // and not worry about forgetting `pop()` in case of problems.
    }
    

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
spioncommented, Apr 28, 2016

Yes thats what the feature is going to be 😃 There will also be co.defer(_ => finallyBlockCode)

Its just that I haven’t had the time to finish the code. I’ll try and finish it up in the next few days.

One thing that might be different is that the code will be:

var doSomeDbStuff = Promise.coroutine(function *() {
  var conn = yield getFromPool().autoDispose();
  ...
  return;  // 'conn' would be released around here
});

Otherwise, its not quite clear that you can’t keep the reference to the connection after the coroutine completes.

1reaction
grabboucommented, Feb 19, 2016

Oh, you guys are quick! Actually decided to write on Twitter because I wasn’t entirely sure about that idea yet.

Just for the reference, what I suggested was:

var connection = yield getConnectionDisposer(options).defer(conn => conn.close());

@spion that makes sense now!

@petkaantonov another interesting idea would be to allow yielding deferred functions at any point inside a go coroutine by wrapping the function with co.defer

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding defer in Go | DigitalOcean
One of the primary uses of a defer statement is for cleaning up resources, such as open files, network connections, and database handles....
Read more >
Deferred - Kotlin
Deferred value is a non-blocking cancellable future — it is a Job with a result. It is created with the async coroutine builder...
Read more >
Defer, Panic, and Recover - The Go Programming Language
Here I'd like to discuss some of the less common ones: defer, panic, and recover. A defer statement pushes a function call onto...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found