2016-08-20 10 views
0

約束が返るまで待つNavigationInstructionをどのように定義できますか?約束が返るまで待つNavigationInstructionを定義する

let loginRedirectRoute: RouteConfig = { 
    name: "openIdRedirectRoute", 
    navigationStrategy: (instruction: NavigationInstruction) => { 
     this.loginRedirectHandler().then(() => { 
      instruction.config.moduleId = openIdConfiguration.LoginRedirectModuleId; 
     }).catch((err) => { 
      console.error(err.message); 
     }); 
    }, 
    route: this.getPath(openIdConfiguration.UserManagerSettings.redirect_uri), 
}; 

上記は機能しません。これは、instruction.config.moduleId ...を同期的に呼び出す場合にのみ機能します。

つまり、約束を返した後に何かを行うナビゲーション戦略が必要です。

これは可能ですか?どうやって?

答えて

2

あなたの内部機能はプロミスを返していません。

this.loginRedirectHandler().then(() => { 
    return instruction.config.moduleId = openIdConfiguration.LoginRedirectModuleId; 
}).catch((err) => { 
    console.error(err.message); 
}); 

  • .then(() => { do_this(); })がないことを

    • .then(() => { return do_this(); })チェーンの約束、
    • .then(() => do_this());チェーンの約束を覚えておいて、試してみてください。

    var foo = 0; 
     
    
     
    function do_this(bar) { 
     
        console.log("call " + foo + " passed " + bar); 
     
        foo++; 
     
        bar++; 
     
        return bar; 
     
    } 
     
    
     
    var promise = new Promise(function(resolve, reject) { 
     
        window.setTimeout(() => { 
     
        resolve(0); 
     
        }, 500); 
     
    }); 
     
    
     
    promise 
     
        .then((bar) => { return do_this(bar); }) /* 1 */ 
     
        .then((bar) => do_this(bar)) /* 2 */ 
     
        .then((bar) => { do_this(bar); }) /* 3 */ 
     
        .then((bar) => do_this(bar)); /* undefined */

  • +0

    コードは '{do_this()の両方で動作します。 } 'と' do_this() 'です。なぜ両方とも機能するのですか?この特定の例では、Promiseを連鎖させる必要はないのですか? –

    +1

    応答遅れ... 次のステップで前回のPromiseの結果を必要としない場合、内部メソッドがPromiseを返さないことは重要ではありません。これは単に、前のPromiseが解決するのを待つことなく、すぐに次のステップが実行されることを意味します。 – JSobell