我正在使用 Firebase 函数创建 API,同时我使用 Firebase Firestore 作为我的数据库。
我正在使用 Node.js 来创建该程序。
我想知道如何在 Node.js 中创建函数。
我将多次调用代码,因为我已经习惯了 Java,而且 Java 具有分子性,在 Node.js 中也可以吗?
这是我的代码
exports.new_user = functions.https.onRequest((req, res) => {
var abc=``;
if(a=='true')
{
abc=Function_A();//Get the results of Function A
}
else
{
abc=Function_B();//Get the results of Function B
//Then Call Function A
}
});
如代码所示,我将根据情况从不同位置调用同一函数两次,然后利用它的结果。
是否可以声明一个函数,然后从不同的位置调用 if ,然后利用其结果?
任何帮助将非常感激,因为我是 Node.js 的新手
如果您尝试从函数返回一个值,则取决于您是进行同步(将 2 个数字相加)还是异步(进行 HTTP 调用)
同步:
let abc = 0;
if(a=='true')
{
abc = Function_A();//Get the results of Function A
}
else
{
abc = Function_B();//Get the results of Function B
//Then Call Function A
}
function Function_B() {
return 2+2;
}
function Function_A() {
return 1+1;
}
异步:
let abc = 0;
if(a=='true')
{
Function_A(function(result) {
abc = result;
});//Get the results of Function A
}
else
{
Function_A(function(result) {
abc = result;
});//Get the results of Function A
}
function Function_B(callback) {
callback(2+2);
}
function Function_A(callback) {
callback(1+1);
}
异步变量:
let abc = 0;
Function_A(2, function(result) {
abc = result; //should by 4
});//Get the results of Function A
function Function_A(myVar, callback) {
callback(myVar * 2);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)