avow icon indicating copy to clipboard operation
avow copied to clipboard

update deps, fix adapter

Open tunnckoCore opened this issue 10 years ago • 8 comments

Fix adapter for Promises/A+ tests. Now with new adapter only 2 tests fails, before that much more Help needed, suggestions?.

tunnckoCore avatar Oct 28 '14 04:10 tunnckoCore

@briancavalier add .all method to test it in Bluebird's benchmark system? I found that http://npm.im/micropromise is 2nd by efficiency, memory and speed (in parallel tests) after bluebird

tunnckoCore avatar Oct 28 '14 04:10 tunnckoCore

Hey @tunnckoCore, thanks for updating this. The changes look simple--I'll try them out today and try to see how easy is might be to get the last two tests to pass.

To be honest, I created avow as an example to help people learn how to implement simple promises. I never intended it to be compliant with the newer Promises/A+ test suite, because I don't really have time to maintain it.

I doubt that avow will be anywhere near as efficient as when.js, bluebird, or rsvp. If you'd like to fork it and add .all so you can test it, please feel free.

briancavalier avatar Oct 28 '14 14:10 briancavalier

Yea, I understand, but why not, it is only one method.

And yea, I digging around different promise libraries to learn and to understand the logic. But I cant get it why some libs that are 1-2kb with simple singleton-like pattern are worst then these that are big and totally logicless.

tunnckoCore avatar Oct 28 '14 14:10 tunnckoCore

@briancavalier also in the description is written "Simple, tiny, fast ...", what mean fast, when we cant make benchmarks. :)

tunnckoCore avatar Oct 28 '14 18:10 tunnckoCore

@tunnckoCore I used a different set of benchmarks at the time I wrote that :) And I haven't really updated the README since then.

briancavalier avatar Oct 28 '14 18:10 briancavalier

Hm yea, okey, sorry! I talk about bluebird's benchmark tests with that I play all night, LOL. And think to decouple them to separate module. But modules must have method that receives arrays and/or .all/.spread

tunnckoCore avatar Oct 28 '14 18:10 tunnckoCore

No worries.

The code in the PR looks good. Only one suggestion: It should be possible to just set the adapter's resolved and rejected to avow.lift and avow.reject, since they really do the same thing.

As for the two tests that are failing, they are testing for circular promise resolution. They check to see if a then callback returns the same promise on which then was called. Eg this case:

var promise = ...;

promise.then(function() {
    return promise;
});

It shouldn't be hard to check for that case. Do you want to give it a shot?

briancavalier avatar Oct 29 '14 19:10 briancavalier

Mm yep, its possible.

As for tests.. yea, I know that, but can't modify it. I try with this rewrite, but continue failing

// Coerce x to a promise
function coerce(x) {
    /* 2.3.1. If promise and x refer to the same object, reject promise with a TypeError as the reason. */
    if (x == this) {
        reject(new TypeError("Promise resolved by its own instance"));
        return;
    }
    /* 2.3.2. If x is a promise, adopt its state */
    if (x instanceof Promise) {
        //x.chain(this);
        return x;
    }

    /* 2.3.3. Otherwise, if x is an object or function,  */
    if (x !== null && (typeof(x) == "object" || typeof(x) == "function")) {
        return promise(function(resolve, reject) {
            enqueue(function() {
                try {
                    // We must check and assimilate in the same tick, but not the
                    // current tick, careful only to access promiseOrValue.then once.
                    var untrustedThen = x.then;

                    if(typeof untrustedThen === 'function') {
                        // Prevent thenable self-cycles
                        call(untrustedThen, x, function(result) {
                            resolve(result === x ? fulfilled(x) : result);
                        }, reject);
                    } else {
                        // It's a value, create a fulfilled wrapper
                        resolve(fulfilled(x));
                    }
                } catch(e) {
                    // Something went wrong, reject
                    reject(e);
                }
            });
        });
    }

    return fulfilled(x);

}

tunnckoCore avatar Oct 29 '14 20:10 tunnckoCore