babel / kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
ISC License
514 stars 41 forks source link

Extra-Tick before sync-function is called #120

Open pubkey opened 7 years ago

pubkey commented 7 years ago

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 commented 7 years ago

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