async await在多个顺序调用上未按预期执行|角打字稿

问题描述 投票:0回答:2

对于每个索引,我触发loadFeed()函数,并期望对实时数据库的每个查询都将按顺序执行。当我在[0、1、2、3、4]行中依次传递5个索引时〜loadFeed(0,10); loadFeed(1,10); loadFeed(2,10)等,输出看起来像这样:

 index rendered: 4 with limitToLast with === 0 
 index rendered: 2 with limitToLast with === 0  
 index rendered: 0 with limitToLast  
 index rendered: 3 with limitToFirst with !== 0
 index rendered: 1 with limitToFirst

期望是这样:

index rendered: 0 with limitToLast
index rendered: 1 with limitToFirst
index rendered: 2 with limitToLast with === 0
index rendered: 3 with limitToFirst with !== 0 
index rendered: 4 with limitToLast with === 0 

有人知道过如何解决这个问题或之前遇到过同样的问题吗?

源代码:

async loadFeed(index, iter) { 

    var that = this;

    // WAITING ROOM FOR THE OBJECTS 
    await this.sortedLoad(index, iter, that); 

  }


  async sortedLoad(index, iter, that) {

    var left =  index % iter;

    if (index === 0 ) {
      return await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToLast(1).once('value', snap => {
        that.cum.push(snap.val());
        console.log('index rendered: ' + index + ' with limitToLast');
        return snap;
      });
    } else if (index === 1) {
      return await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToFirst(1).once('value', snap => {
        that.cum.push(snap.val());
        console.log('index rendered: ' + index + ' with limitToFirst');
        return snap;
      });
    } else {

      if (left === iter-1) {
        return await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToFirst(1).once('value', snap => {
          that.cum.push(snap.val());
          console.log('index rendered: ' + index + ' with limitToFirst');
          return snap;
        });
      } else {

        if ( index % 2 === 0 ) { // EVEN
          return await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToLast(1).once('value', snap => {
            that.cum.push(snap.val());
            console.log('index rendered: ' + index + ' with limitToLast with === 0');
            return snap;
          });
        } else if ( index % 2 !== 0 ) { // ODD
          return await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToFirst(1).once('value', snap => {
            that.cum.push(snap.val());
            console.log('index rendered: ' + index + ' with limitToFirst with !== 0');
            return snap;
          });
        } 

      }

    }
  }
// -----

调用loadFeed()的函数

public cum = []; // render url results
private outputListener  = new BehaviorSubject<any[]>([]);
private obs: Observable<any>;

      this.obs = this.outputListener.asObservable().scan( (acc, val) => {
        //if (String(val[0]) === 'refresh') {
          return acc.concat(val);
      });

      // EACH NEW ITEM HITS HERE
      this.obs.subscribe(data => {

        this.items = data.length;
        const iter = 10;
        const index = this.items - 1;  // true array index
        if (index > -1) {
          this.loadFeed(index, iter); // KICK OFF EACH LOAD HERE
        }
      });

      this.outputListener.next([i]); // Trigger next item
angular typescript firebase-realtime-database async-await chaining
2个回答
-1
投票

您返回异步函数的方式是错误的。删除return await子句,然后单独添加await

if (index === 0 ) {
 await that.rdb.database.ref('f/u/' + that.gl.currentUser + '/').orderByChild('date').limitToLast(1).once('value', snap => {
    that.cum.push(snap.val());
    console.log('index rendered: ' + index + ' with limitToLast');
    return snap;
  });
}

return await子句返回promise而不是值。

© www.soinside.com 2019 - 2024. All rights reserved.