hnews.getById(id)
を使用してJSONオブジェクトを返す関数があります。私は約束に返された各話を配列にプッシュします。私はどのようにして完全な配列を得るのか分かりません。別の約束をする必要がありますか?プロミスから完全な配列を返します
function updateTopStories() {
var storiesArr = [];
hnews.getIdsInCategory('topstories', 60)
.then(function(ids) {
ids.forEach(function(id) {
hnews.getById(id).then(function(story) {
console.log(story);
storiesArr.push(story);
});
});
});
return storiesArr;
}
var stories = updateTopStories();
console.log(stories); // Empty array
編集:私はstoriesArr
第二EDIT
updateTopStories();
からを返す必要があります:私は馬鹿です。
getById
はPromiseを返します。私は寝る時間だと思う。 modはこれを削除してくださいか?
ここから取り出せます。探してくれてありがとう。
あなたは 'サンプルあなたが得る出力と予想される出力 –
' hnews.getById(ID)を貼り付けることができますが約束またはJSONの配列を返しますか? – Agalo
@Agalo 'hnews.getById(id)'はJSONオブジェクトを返します –