0

doh.Deferredを使用して、次の一連のイベントをチェックするテストを作成しようとしています。

  1. ユーザー A でログイン (非同期)
  2. ログアウト (同期)
  3. ユーザー A でログイン (非同期)

2 番目のコールバック関数の戻り値は、別の doh.Deferred オブジェクトです。d のコールバック チェーンは d2 を待機するという印象を受けましたが、そうではありません。テストは d2.callback が呼び出される前に終了します。

ここでどこが間違っていますか?

この動作をテストするためのより良い方法を知っている人はいますか?

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;
}
4

1 に答える 1

0

これは機能します。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;
}
于 2009-06-17T15:31:21.147 に答える