Giter Site home page Giter Site logo

Possible async problem about empower HOT 3 OPEN

jamestalmage avatar jamestalmage commented on June 19, 2024
Possible async problem

from empower.

Comments (3)

twada avatar twada commented on June 19, 2024

@jamestalmage Ah, Good catch! Thank you for sharing your thought!

import assert from 'power-assert';

const tests = [];

function test (name, body) {
    tests.push({
        name,
        body
    });
}

function later (millis, value) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(`captured [${value}]`);
            resolve(value);
        }, millis);
    });
};

test('testA', async () => {
    const val1 = 'a1';
    const val2 = 'a2';
    const delay1 = 1000;
    const delay2 = 6000;
    try {
        assert.equal(await later(delay1, val1), await later(delay2, val2));
    } catch (e) {
        console.log(e.message);
    } finally {
        console.log(`done [testA]`);
    }
});

test('testB', async () => {
    const val3 = 'b1';
    const val4 = 'b2';
    const delay3 = 2000;
    const delay4 = 3000;
    try {
        assert.equal(await later(delay3, val3), await later(delay4, val4));
    } catch (e) {
        console.log(e.message);
    } finally {
        console.log(`done [testB]`);
    }
});

for (let {name, body} of tests) {
    console.log(`running [${name}]`);
    body();
}

Produces power-assert output that is totally broken.

$ babel-node interleaving_promises.js
running [testA]
running [testB]
captured [a1]
captured [b1]
captured [b2]
  # interleaving_promises.js:41

  assert.equal(await later(delay3, val3), await later(delay4, val4))
               |                          |           |       |
               |                          |           |       |a2"
               |                          |           |000    "b2"
               "b1"                       "b2"        3000

done [testB]
captured [a2]
  # interleaving_promises.js:27

  assert.equal(await later(delay1, val1), await later(delay2, val2))
               |           |       |      |
               "a1"        2000    "b1"   "a2"

done [testA]
$ 

I'll fix this problem in this issue

from empower.

jamestalmage avatar jamestalmage commented on June 19, 2024

Thanks @twada, I meant to write that test and verify this.

Could we maybe introduce a runtime helper? I think Babel 6 now includes an easy way to do that (from what I have browsed of the source.

t.is(await foo(), await bar());
var __powerAssertRuntime = require(...); // once at the top of the file.

 // create a unique name - I think Babel might have a tool for this.
const capt_1 = new __powerAssertRuntime.Capture(); 
const capt_2 = new __powerAssertRuntime.Capture(); 

t.is(await capt_1._expr(capt_1._capt(foo(), {powerAssertContext:...})), await capt_2._... )

from empower.

twada avatar twada commented on June 19, 2024

@jamestalmage Yeah, I'm thinking about introducing runtime helper too.

This is my thought but I'll make prototypes of both.

from empower.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.