Proposal: defer keyword

Sultan thysultan at gmail.com
Thu Sep 20 10:42:11 UTC 2018


What stops you from doing this with try...finally?

function doSomeDbWork() {
    try { return databasepool.getConnection(); } finally {
connection.release(); }
}


On Thu, Sep 20, 2018 at 1:01 PM, Ayush Gupta <ayushg3112 at gmail.com> wrote:

> Apologies, I meant to use `async-await` in the example but I missed it.
>
> Also, cleanup can be needed in all code, no matter if it's synchronous,
> uses promises, callbacks, or `async-await`.  I personally believe that
> while we can have different mechanisms for doing cleanup  in all different
> cases, having a single standard mechanism is better.
>
> On Thu, Sep 20, 2018 at 3:09 PM Ben Wiley <therealbenwiley at gmail.com>
> wrote:
>
>> If you don't need to do anything in the catch block, you can make it a
>> no-op and put your cleanup statement afterward.
>>
>> However framing this in terms of a synchronous use case seems odd given
>> that synchronous database operations (your example) are extremely rare and
>> in JavaScript. It seems to me like promises would fit this case pretty
>> well, but I may have missed something.
>>
>> Ben
>>
>> Le jeu. 20 sept. 2018 05 h 32, Ayush Gupta <ayushg3112 at gmail.com> a
>> écrit :
>>
>>> It can be covered, but then I'll have to duplicate the
>>> `connection.release()` call in both the try and catch blocks, (and
>>> remember, there can be multiple resources to be cleaned up).
>>>
>>> Plus, in case that I have a function with multiple if-else branches with
>>> returns from multiple branches, I will have to duplicate that in all the
>>> branches.
>>>
>>> Another benefit of this is that this will help us logically group
>>> allocation and deallocation of resources together, for better readability
>>> and debuggability.
>>>
>>> On Thu, Sep 20, 2018 at 2:57 PM Ben Wiley <therealbenwiley at gmail.com>
>>> wrote:
>>>
>>>> Hey Ayush,
>>>>
>>>> That's an interesting language feature I hadn't heard of before.
>>>>
>>>> Any reason your use case couldn't be covered by a try/catch in the
>>>> synchronous case, and a promise.finally() in the async case?
>>>>
>>>> Ben
>>>>
>>>> Le jeu. 20 sept. 2018 05 h 21, Ayush Gupta <ayushg3112 at gmail.com> a
>>>> écrit :
>>>>
>>>>> Hi,
>>>>>
>>>>> I would like to propose a `defer` keyword(or something else  as the
>>>>> keyword name) which would allow   us to "defer" a function which will be
>>>>> executed once the current function either returns or throws.
>>>>>
>>>>> The idea for this is taken from the Go programming language.
>>>>>
>>>>> It would allow us to perform cleanup activities in a function which
>>>>> has multiple branches in a single place.
>>>>>
>>>>> For example, a sample server side code can look  like:
>>>>>
>>>>> ```  js
>>>>> function doSomeDbWork() {
>>>>>     const connection = databasepool.getConnection();
>>>>>     defer function () { connection.release(); } // function would be
>>>>> called no matter when/if the function returns or throws
>>>>>     //   do your work
>>>>> }
>>>>> ```
>>>>>
>>>>> Regards
>>>>> Ayush Gupta
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>>
>>>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180920/d4af16c2/attachment.html>


More information about the es-discuss mailing list