Question

J'essaie d'utiliser doh.Deferred pour écrire un test qui vérifiera la séquence d'événements suivante:

  1. connexion avec l'utilisateur A (asynchrone)
  2. déconnexion (synchrone)
  3. connexion avec l'utilisateur A (asynchrone)

La valeur de retour de la deuxième fonction de rappel est un autre objet doh.Deferred. J'avais l'impression que la chaîne de rappel de d attendra d2, mais ce n'est pas le cas. Le test se termine avant que d2.callback ne soit appelé.

Où est-ce que je vais mal ici?

Quelqu'un sait-il qu'il existe un meilleur moyen de tester ce comportement?

function test() {
    var d = new doh.Deferred();

    d.addCallback(function() {  
        Comm.logout(); /* synchronus */
        try {   
            // check with doh.t and doh.is
            return true;
        } catch (e) {
            d.errback(e);
        }
    });

    d.addCallback(function() {
        var d2 = new dojo.Deferred();
        /* asynchronus - third parameter is a callback */
        Comm.login('alex', 'asdf', function(result, msg) {
                try {
                    // check with doh.t and doh.is
                    d2.callback(true);
                } catch (e) {
                    d2.errback(e);
                }                   
            });
        return d2; // returning doh.Defferred -- expect d to wait for d2.callback
    });     

    /* asynchronus - third parameter is a callback */
    Comm.login('larry', '123', function (result, msg) {
        try {
            // check with doh.t and doh.is 
            d.callback(true);
        } catch (e) {
            d.errback(e);
        }
    }); 

    return d;
}
Était-ce utile?

La solution

Cela fonctionne. Le problème était lié à d2.

function test() {
    var d = new doh.Deferred();
    var d2 = new doh.Deferred();

    d.addCallback(function() {  
        Comm.logout(); /* synchronus */
        try {   
                // check with doh.t and doh.is
                return true;
        } catch (e) {
                d.errback(e);
        }
    });

    d.addCallback(function() {
        /* asynchronus - third parameter is a callback */
        Comm.login('alex', 'asdf', function(result, msg) {
                        try {
                                // check with doh.t and doh.is
                                d2.callback(true);
                        } catch (e) {
                                d2.errback(e);
                        }                                       
                });
        return d2; // returning doh.Deferred -- waits for d2.callback
    });         

    /* asynchronus - third parameter is a callback */
    Comm.login('larry', '123', function (result, msg) {
        try {
                // check with doh.t and doh.is 
                d.callback(true);
        } catch (e) {
                d.errback(e);
        }
    }); 

    return d;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top