简体   繁体   English

为什么 Firebase 云 Function 执行定义的 Promise

[英]Why does Firebase Cloud Function execute defined Promise

I'm trying create a Promise that I can use later.我正在尝试创建一个稍后可以使用的 Promise。 However, that information seems irrelevant because the Promise executes without invocation.但是,该信息似乎无关紧要,因为 Promise 无需调用即可执行。 Why is the Promise invoked automatically?为什么会自动调用 Promise?

const functions = require("firebase-functions");

const p = new Promise(function (resolve) {
  setTimeout(function () {
    console.trace("server generating random number...");
    resolve(Math.random());
  }, 1000);
});

module.exports.MyCustomFunction = functions.https.onRequest(function (req, res) {
  res.send("hello world");
});

When I run the Firebase functions and hosting locally, the promise is executed without invoking the promise or invoking the cloud function.当我运行 Firebase 函数并在本地托管时,执行 promise 而不调用 promise 或调用云 ZC141145268C163。 When I invoke the cloud function, the Promise is executed again.当我调用云 function 时,再次执行 Promise。

$  firebase serve --only functions,hosting

   i  functions: Watching "/code/project" for Cloud Functions...
   i  hosting: Serving hosting files from: dist/client
   ✔  hosting: Local server: http://localhost:5000
   ✔  functions[MyCustomFunction]: http function initialized (...).

   >  server generating random number...

...

   i  functions: Finished "MyCustomFunction" in ~1s
   >  server generating random number...

I do not expect the Promise to run at all.我不希望 Promise 能够运行。 I would only to expect the Promise to run when I call p() The code I posted is the exact code that is running我只希望 Promise 在我调用p()时运行 我发布的代码是正在运行的确切代码

The moment your code executes new Promise , or call any method that returns a promise, the work of that promise has been started.当您的代码执行new Promise或调用任何返回 promise 的方法时,该 promise 的工作已经开始。 You don't have to call then or catch on a promise to get it to do work.你不必打电话或catch then来让它工作。

This is not unique to Cloud Functions.这不是 Cloud Functions 独有的。 This is just the way JavaScript works.这就是 JavaScript 的工作方式。

const p = function(){
      new Promise(function (resolve) {
      setTimeout(function () {
        console.log("server generating random number...");
        resolve(Math.random());
      }, 1000);
  })
};
p();

It works like this.它是这样工作的。 Maybe it is because you have defined the variable.The promise is acting like the Initial value for 'P' as the program loads.也许是因为您已经定义了变量。promise 在程序加载时就像“P”的初始值一样。 Try Using a different approach.尝试使用不同的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义 Cloud Firebase 函数错误承诺 - Cloud firebase function error promise is not defined 为什么这个异步 function 在它之前定义的等效 Promise.then 链之前执行? - Why does this async function execute before the equivalent Promise.then chain defined before it? 为什么 firebase cloud-function javascript promise 运行次数超过循环调用次数? - Why does firebase cloud-function javascript promise run more than the number of loop invocations? 为什么当无服务器 function 在 Firebase 云 Z2764085932F8B 中成功完成 Promise.all() 时退出时性能会下降? - Why does performance degrade when a serverless function exits without completing Promise.all() successfully in Firebase Cloud Function? 为什么我的函数在我的 Promise 回调之前执行? - Why does my function execute before my promise callback? Firebase 云函数:事务 function 不返回 promise? - Firebase Cloud Functions: Transactions function not returning promise? 为什么我的代码执行return语句后定义的函数? - Why does my code execute a function defined after the return statement? Firebase 云 Function 偶尔不执行 - Firebase Cloud Function occassionally doesn't execute Firestore Cloud Function:出现“ReferenceError:firebase 未定义” - Firestore Cloud Function: Getting “ReferenceError: firebase is not defined” 为什么“解析云代码”中的此Promise无效? - Why does this Promise in Parse Cloud Code not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM