:bird: Feature (coroutines): Support yielding autoDisposing disposers (like Go defer)
See original GitHub issueSince 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 ifautoDispose()
is that clear? Defer is also more generic - you could schedule any sort of cleanup with it. IfPromise.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:
- Created 8 years ago
- Comments:14 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Otherwise, its not quite clear that you can’t keep the reference to the connection after the coroutine completes.
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:
@spion that makes sense now!
@petkaantonov another interesting idea would be to allow yielding deferred functions at any point inside a
gocoroutine by wrapping the function with co.defer