LoginSignup
3
0

More than 5 years have passed since last update.

GCPのCloud Functionsでの、関数の外の変数について

Posted at

CloudFunctions便利ですよね。
デプロイの通知からpuppeteerを使ってみたりまで、色々と遊べる範囲が広いのです。

そんなCloud Functionsなんですが……ふと、呼び出される関数の外に定義してある変数を操作したらどうなるのだろうという疑問が出てきたので試してみました。

対象のプログラム!

cf_var.js
let count = 0;

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.addCount = (req, res) => {
  count += 1;
  res.status(200).send(`count ${count}`);
};

対象のCloud Functions!

踏めば踏むほど加算されていくので、F5を連打する甲斐があるッ!

一度起動したら、そのコンテナの中で処理を実行させ続けているからかな?
ある程度、時間が経過したりしたら消えそうな気がする:thinking:

これを使って何か……できそうな気がするんだけど思い浮かばない。

3
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0