我们如何定义一个等待promise返回的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时才有效。
换句话说,我们需要一种导航策略,在承诺返回后执行某些操作。
这可能吗?怎么样?
答案 0 :(得分:3)
你的内心功能没有返回一个承诺。尝试
this.loginRedirectHandler().then(() => {
return instruction.config.moduleId = openIdConfiguration.LoginRedirectModuleId;
}).catch((err) => {
console.error(err.message);
});
请记住
.then(() => { return do_this(); })
连锁承诺,.then(() => 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 */