Javascript Async/Await,如何强制函数在承诺得到解决后再继续?

Javascript Async/Await,如何强制函数在承诺得到解决后再继续?,javascript,typescript,asynchronous,async-await,Javascript,Typescript,Asynchronous,Async Await,我希望我的函数一直执行到nextPageToken为null。当我第一次执行函数时,它会等待承诺得到解决。但是,一旦响应上出现nextPageToken,函数就不会等待响应,并且会发生堆栈溢出 似乎f()没有挂起在when wait p上。然后()被调用 我是否完全误解了async/await的工作原理 任何帮助都将不胜感激 public apiResult2(path: string, objectName: string, params: any = { }) { let retur

我希望我的函数一直执行到nextPageToken为null。当我第一次执行函数时,它会等待承诺得到解决。但是,一旦响应上出现nextPageToken,函数就不会等待响应,并且会发生堆栈溢出

似乎f()没有挂起在when wait p上。然后()被调用

我是否完全误解了async/await的工作原理

任何帮助都将不胜感激

public apiResult2(path: string, objectName: string, params: any = { }) {
    let returnArray = [];
    const p = new Promise<any> ((resolve, reject) => {
      gapi.load('auth2:client', () => {
        gapi.client.request({
          path: path,
          params: params
        }).then(response => {
          // resolve this promise with the first key in the response object.
          resolve(response.result);
        }, reason => {
          console.log(reason);
          reject(reason.result);
        });
      });
    });
    let f = async () => {
      let nextPageToken = null;
      do {
        let r = await p.then(result => {
          if (result.hasOwnProperty(objectName)) {
            for (let obj of result[objectName]) {
              returnArray.push(obj);
            }
          }
          if (result.hasOwnProperty('nextPageToken')) {
            params.nextPageToken = result.nextPageToken;
            return result.nextPageToken;
            // nextPageToken = result.nextPageToken;
          } else {
            params.nextPageToken = null;
            return null;
            // nextPageToken = null;
          }
        });
        nextPageToken = r;
        console.log(r);
      } while (nextPageToken);
    };
    f();
    return returnArray;
  }
public apiResult2(路径:string,对象名:string,参数:any={}){
让returnArray=[];
const p=新承诺((解决、拒绝)=>{
load('auth2:client',()=>{
gapi.client.request({
路径:路径,
params:params
})。然后(响应=>{
//使用响应对象中的第一个密钥解析此承诺。
决心(回应、结果);
},原因=>{
控制台日志(原因);
拒绝(理由、结果);
});
});
});
设f=async()=>{
设nextPageToken=null;
做{
让r=等待p.then(结果=>{
if(result.hasOwnProperty(objectName)){
for(让结果[objectName]的obj){
returnArray.push(obj);
}
}
if(result.hasOwnProperty('nextPageToken')){
params.nextPageToken=result.nextPageToken;
返回result.nextPageToken;
//NEXTPGETOKEN=结果。NEXTPGETOKEN;
}否则{
params.nextPageToken=null;
返回null;
//nextPageToken=null;
}
});
NEXTPGETOKEN=r;
控制台日志(r);
}while(nextPageToken);
};
f();
返回数组;
}
如果函数需要“等待”某个异步调用,那么它也必须是异步的。您的函数
apirresult2
不会等待
f
完成,以便
返回returnArray

编辑:

这里的主要问题是,您试图重用promise
p
来发出不同的请求,但这是不可能的。promise
p
将使用第一个请求的参数初始化,所有对
p的调用都将以相同的结果完成:第一页请求的响应

我对您的代码做了一些小更改,并使其与模拟界面一起工作:

const apiResult2 = async (path: string, objectName: string, params: any = { }) => {
    const requestPage = async () => new Promise<any> ((resolve, reject) => {
        gapi.load('auth2:client', () => {
            gapi.client.request({
                path: path,
                params: params
            }).then(response => {
                // resolve this promise with the first key in the response object.
                resolve(response.result);
            }, reason => {
                console.log(reason);
                reject(reason.result);
            });
        });
    });

    let returnArray: string[] = [];
    do {
        const page = await requestPage();

        if (page.hasOwnProperty(objectName)) {
            for (let obj of page[objectName]) {
                returnArray.push(obj);
            }
        }

        if (page.hasOwnProperty('nextPageToken')) {
            params.nextPageToken = page.nextPageToken;
        } else {
            params.nextPageToken = null;
        }
    } while (params.nextPageToken);

    return returnArray;
}

我尝试将
apireult2()
作为一个异步函数,将returnArray移动到
f()
中并返回它,然后添加
returnawait f()但是,结果仍然相同。这里是我调用apiResult2的地方,如果有帮助的话
ngOnInit(){this.sub=this.route.params.subscribe(params=>{this.students=this.googleApi.apirect2('https://classroom.googleapis.com/v1/courses/“+params['id']+'/students”,“students”);}
非常感谢!!!函数现在等待第一个Api调用完成,然后再请求下一个Api调用。当我使用新函数时,发生了两件意想不到的事情。1.每次函数有趣时,都会同时调度2个相同的api调用。2.当我console.log返回值时,结果是一个承诺而不是一个数组?:
(ZoneAwarePromise{uuuuuuuuu-zone\u-symbol\uuuuuu-state:null,{uuuuuuuu-zone\u-symbol\uuuuuuu-state:null,{uuuuuuuuuuuuuuu-zone\u-symbol\uuuuuu-value:Array(0)}uuuuuuuu-symbol\uuuu-state:true\uuuuuu-symbol\uu-value:Ar。。。有什么想法吗?啊,无视上面的评论。查看使用示例,我可以看到我需要解析调用函数中的承诺!工作如期!!非常感谢。
apiResult2(path, objectName, params).then(
    result => console.log(result),
    err => console.log('error', err)
);