Google Cloud Functions(GCP)を使用して、functions.http に await を組み込む方法をここにメモします。
Google Cloud Functions を利用する際、http リクエストを処理する関数は、onRequest メソッドで定義します。この関数に対して、async/await を使用して非同期処理を行うことができます。
以下サンプルコードです。
const fetch = require('node-fetch'); // HTTPトリガーの関数を定義 exports.httpFunction = async (req, res) => { try { // 外部APIにリクエストを送り、レスポンスを取得 const response = await fetch('https://jsonplaceholder.typicode.com/posts/1'); // レスポンスが正常でない場合はエラーをスロー if (!response.ok) { throw new Error(`HTTPエラー: ${response.status}`); } // レスポンスをJSONに変換 const data = await response.json(); // 成功レスポンスを返す res.status(200).json({ success: true, data: data }); } catch (error) { // エラーハンドリング res.status(500).json({ success: false, message: error.message }); } };