Javascript 如何在继续之前等待异步回调方法?
我有以下两种方法 但是在我从Http模块重写的Javascript 如何在继续之前等待异步回调方法?,javascript,typescript,callback,Javascript,Typescript,Callback,我有以下两种方法 但是在我从Http模块重写的get方法中,身份验证成功回调在它已经执行请求并返回响应后被调用。这样就增加了 JWT令牌以错误的顺序发送到头,而且太晚了 我对承诺和观察不太了解。。但是我该怎么做才能让它在执行请求和返回响应之前真正等待回调完成呢 authenticate(authCompletedCallback, errorCallback) { let authContext = new Microsoft.ADAL.AuthenticationContext(AUTHO
get
方法中,身份验证成功回调在它已经执行请求并返回响应后被调用。这样就增加了
JWT令牌以错误的顺序发送到头,而且太晚了
我对承诺和观察不太了解。。但是我该怎么做才能让它在执行请求和返回响应之前真正等待回调完成呢
authenticate(authCompletedCallback, errorCallback) {
let authContext = new Microsoft.ADAL.AuthenticationContext(AUTHORITY_URL);
authContext.tokenCache.readItems().then((items) => {
if (items.length > 0) {
AUTHORITY_URL = items[0].authority;
authContext = new Microsoft.ADAL.AuthenticationContext(AUTHORITY_URL);
}
// Attempt to authorize user silently.
authContext
.acquireTokenSilentAsync(RESOURCE_URL, CLIENT_ID)
.then(authCompletedCallback, () => {
// We require user credentials so trigger authentication dialog.
authContext
.acquireTokenAsync(RESOURCE_URL, CLIENT_ID, REDIRECT_URL)
.then(authCompletedCallback, errorCallback);
});
});
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
this.authenticate((authResponse) => {
// This is executed second.
if (!options) {
options = { headers: new Headers() };
}
options.headers.set('Authorization', 'Bearer ' + authResponse.accessToken);
}, (err) => {
alert(JSON.stringify(err));
});
// This is executed first.
return super.get(url, options);
}
身份验证(authCompletedCallback,errorCallback){
让authContext=new Microsoft.ADAL.AuthenticationContext(AUTHORITY\u URL);
authContext.tokenCache.readItems()。然后((项)=>{
如果(items.length>0){
AUTHORITY\u URL=items[0]。权限;
authContext=new Microsoft.ADAL.AuthenticationContext(AUTHORITY\u URL);
}
//尝试以静默方式授权用户。
authContext
.acquireTokenSilentAsync(资源URL、客户端ID)
.然后(authCompletedCallback,()=>{
//我们需要用户凭据,以便触发身份验证对话框。
authContext
.acquireTokenAsync(资源\u URL、客户端\u ID、重定向\u URL)
.然后(authCompletedCallback,errorCallback);
});
});
}
获取(url:string,options?:RequestOptionsArgs):可观察{
this.authenticate((authResponse)=>{
//这是第二次执行。
如果(!选项){
选项={headers:newheaders()};
}
options.headers.set('Authorization','Bearer'+authResponse.accessToken);
},(错误)=>{
警报(JSON.stringify(err));
});
//这是首先执行的。
返回super.get(url、选项);
}
您可能希望让get函数返回一个承诺,因为实际的响应对象在您进行身份验证之前不应该存在。不过,看起来您正在重写一个方法,因此您可能需要创建一个不同的方法
getWithAuthentication(url: string, options?: RequestOptionsArgs): Promise<Observable<Response>> {
return new Promise((resolve, reject) => {
this.authenticate((authResponse) => {
if (!options) {
options = { headers: new Headers() };
}
options.headers.set('Authorization', 'Bearer ' + authResponse.accessToken);
let response = super.get(url, options);
resolve(response);
}, (err) => {
alert(JSON.stringify(err));
reject(err);
});
}
}
getWithAuthentication(url:string,options?:RequestOptionsArgs):承诺{
返回新承诺((解决、拒绝)=>{
this.authenticate((authResponse)=>{
如果(!选项){
选项={headers:newheaders()};
}
options.headers.set('Authorization','Bearer'+authResponse.accessToken);
让response=super.get(url,选项);
决心(回应);
},(错误)=>{
警报(JSON.stringify(err));
拒绝(错误);
});
}
}
请参见,现在我将调用http.get()
,而不是调用http.getWithAuthentication().then()
?是的,这就是想法。或者如果您想使用异步代码,可以使用wait。