Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

Extra-Tick before sync-function is called #120

Open
pubkey opened this issue Sep 25, 2017 · 1 comment
Open

Extra-Tick before sync-function is called #120

pubkey opened this issue Sep 25, 2017 · 1 comment

Comments

@pubkey
Copy link

pubkey commented Sep 25, 2017

Source:

    async wrapCall(fun) {
        const unlock = this.lock();
        let ret;
        try {
            console.log('wrapCall() ' + this._queueCounter);
            ret = await fun();
        } catch (err) {
            // not sucessfull -> unlock before throwing
            unlock();
            throw err;
        }
        // sucessfull -> unlock before return
        unlock();
        return ret;
    },

Is transpiled to:

wrapCall: function wrapCall(fun) {
        var unlock,
            ret,
            _this3 = this;

        return Promise.resolve().then(function () {
            unlock = _this3.lock();
            ret = void 0;
            return Promise.resolve().then(function () {
                console.log('wrapCall() ' + _this3._queueCounter);
                return fun();
            }).then(function (_resp) {
                ret = _resp;
            }).catch(function (err) {
                // not sucessfull -> unlock before throwing
                unlock();
                throw err;
            });
        }).then(function () {
            // sucessfull -> unlock before return
            unlock();
            return ret;
        });
    }

The problem here is that this.lock(); is called after the next tick instead of instantly.
This creates a different side-effect than expected.
Since the lock-call is synchronous it should transpile to something like:

wrapCall: function wrapCall(fun) {
        var unlock,
            ret,
            _this3 = this;
       unlock = _this3.lock();
        return Promise.resolve().then(function () {
         // etc...

```js
@loganfsmyth
Copy link
Member

Thanks for filing. Beware that this module is essentially unmaintained currently. Happy to leave this open, but it probably won't get fixed.

You may want to take a look at https://www.npmjs.com/package/fast-async

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants