fetchAPIの基本的な動作で不明な点があり、ご教示いただきたいです。
(promiseの理解が不正確であることが原因かもしれないです。)
まず以下のようにレスポンスをjsonオブジェクト化したものをコンソール出力できることを確認しました。
fetch(`https://swapi.co/api/films/1`) .then(res => res.json()) .then(json => console.log(json)) => Promise {<pending>} => VM3928:3 {title: "A New Hope", episode_id: 4, opening_crawl: "It is a period of civil war. ↵Rebel spaceships, st…er ↵people and restore ↵freedom to the galaxy....", director: "George Lucas", producer: "Gary Kurtz, Rick McCallum", …}
続いて、2つのthenを1つにまとめることができるのではないかと思い試したところ、以下のような結果となりました。
fetch(`https://swapi.co/api/films/1`) .then(res => console.log(res.json())) => Promise {<pending>} => VM3881:2 Promise {<pending>}
この場合、先程jsonオブジェクトを返していたres.json()
が、Promiseオブジェクトを返しているように思われます。
MDN等のリファレンスを見ても理由が分からなかったため質問した次第です。
ご教示のほどよろしくお願いします????
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/14 17:11
2020/02/14 23:33
2020/02/15 04:08
2020/02/16 09:16