【发布时间】:2020-12-10 06:55:45
【问题描述】:
我正在尝试创建一个以后可以使用的 Promise。但是,该信息似乎无关紧要,因为 Promise 无需调用即可执行。 为什么会自动调用 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");
});
当我在本地运行 Firebase 函数并托管时,会在不调用 Promise 或调用云函数的情况下执行 Promise。当我调用云函数时,再次执行 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...
我根本不希望 Promise 运行。我只希望 Promise 在我调用 p() 时运行我发布的代码是正在运行的确切代码
【问题讨论】:
-
你认为是不是
const p = new Promise(function (resolve) { ...... })不是被“调用”......它是 - 然后你继续说when I call p()...... 但 be 是一个承诺,而不是一个功能- 所以“调用 p()”会导致错误 - 很惊讶你没有看到
标签: javascript firebase promise google-cloud-functions