※2022/08/30更新
複数のgetリクエストを連続して実行する場合、非同期的に実行するとコールバックにばらばらに帰ってきてしまう。
getリクエストの結果を受け取った後に、次のリクエストを実行するようなロジックを探す必要があった。
node-fetchを活用して、下記のようなソースで実行すればよい。
www.npmjs.com
//index.js import fetch from 'node-fetch'; const urls = [""/*1つめのURL*/, ""/*2つめのURL*/]; for(const url of urls) { const response = await fetch(url); const body = await response.text(); console.log(body.length); }
//package.json { "name": "fetch-sample", "type": "module", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "node-fetch": "^3.2.10" } }