2016-08-01 4 views
3

私は、ゲーム中のキルに関するすべての詳細を返すAPIを打ちました。最初のエンドポイントはkillイベントにidを返します。キラーと殺害された名前を取得するためにヒット。親httpリクエストで完了したすべての子HTTPリクエストを一度返す方法

:このため、APIは私が最初のイベントIDを取得し、その後、結果を取得し、全体キル配列を処理するために、返される配列内のすべてのidのを待つための要求を作成する必要が設​​定されている方法の

requestify.get(url).then(function (response) { 
     var events = []; 
     if (response.body && response.body.length > 0) { 
      data = JSON.parse(response.body); 
      if (data.hasOwnProperty('events')) { 
       events = data.events.map(function(event) { 
        return this.getDataForHeroKillId(event.id, function(killInfo) { 
         return { killer: killInfo.killer, killed: killInfo.killed, timestamp: event.time }; 
        }); 
       }.bind(this)); 
       console.log('events is: ', events); 
      } 
     } 
     return Promise.all(events); 
    }.bind(this)); 

マイgetKillInformation機能は次のようになります。私は、私はそれぞれの子の要求の結果をコールバックができ、それらはすべて実行された後、その後、私の新しい配列は、データを保持することを期待していた第2の方法では

KillFeed.prototype.getKillInformation = function(id, cb) { 
var data = null; 
    requestify.get(url).then(function (response) { 
     var event = {}; 
     if (response.body && response.body.length > 0) { 
      data = JSON.parse(response.body); 
      if (data.hasOwnProperty('Killer')) { 
       event = { killer: data.Killer, killed: data.Killed}; 
      } 
     } 
     cb(event); 
    }); 
}; 

。しかし、JSのイベント駆動型の性質のため、私のコードブロックは空のイベント配列を返すことがわかりました。なぜなら、このコードは明らかに非ブロッキングです(明らかに、HTTPリクエストを作成するのは理想的ではありません。これをどのように実装できますか?

答えて

1

ワンはこれにpromisesを使用します。

requestify.get(url).then(function (response) { 
    var events = []; 

    if (response.body && response.body.length > 0) { 
     var data = JSON.parse(response.body); 
     if (data.hasOwnProperty('events')) { 
      // Trying to process the kill information here 
      events = data.events.map(function(event) { 
       return this.getKillInformation(event.id).then(function(killInfo) { 
        return { killer: killInfo.killer, killed: killInfo.killed, timestamp: event['time1'] }; 
       }); 
      }.bind(this)); 
     } 
    } 

    return Promise.all(events); 
}); 

KillFeed.prototype.getKillInformation = function(id) { 
    var url = 'internal_url'; 
    return requestify.get(url).then(function (response) { 
     if (response.body && response.body.length > 0) { 
      var data = JSON.parse(response.body); 
      if (data.hasOwnProperty('killer')) { 
       return { killer: data.Killer, killed: data.Killed }; 
      } 
     } 
    }); 
}; 
+0

私の 'events.map'ループではお返事ありがとうございます。それは[undefined、undefined、undefined]に解決されました。 HTTP要求が解決される前にその項目を返すので、 'return Promise.all'は配列' [undefined、undefined、undefined] 'を返します。 – Alex

+0

@Alexイベントあなたの条件が成就しないため、約束事ではなく、未定義のイベントが返されます。 'getKillInformation' response.bodyの長さは0であるか、* killer *プロパティはありません。値は返されません。デフォルトでは、undefinedが返されます。 – Kaspars

+0

私は実際に '.getKillInformation'を約束する必要があると考えていますので、Promise.allは解決するか拒否するかを知っています。現在の設定では、 'getKillInformation'からのコールバックが呼び出されると、情報は' .map'関数で期待どおりに出力されますが、マップイベントが終了する前にイベント配列が返されることがわかります – Alex

0

asyncとそのwaterfallメソッドを使用できます。 AsyncはNodeJSモジュールですが、ブラウザでも使用できます。

関連する問題