我试图对我的节点js代码中的函数进行同步调用。节点js中的同步函数调用
我打电话给我的功能,这样
set_authentication();
set_file();
function set_authentication(){
---
callback function
---
}
我想,我的set_authentication()函数应首先执行完全,然后set_file()应该开始执行,但set_file()函数开始set_authentication的回调之前执行()。
我曾尝试使用这种异步也喜欢
async.series(
[
// Here we need to call next so that async can execute the next function.
// if an error (first parameter is not null) is passed to next, it will directly go to the final callback
function (next) {
set_looker_authentication_token();
},
// runs this only if taskFirst finished without an error
function (next) {
set_view_measure_file();
}
],
function(error, result){
}
);
,但它也不起作用。
我试过承诺也
set_authentication().then(set_file(),console.error);
function set_authentication(){
---
callback function
var myFirstPromise = new Promise((resolve, reject) => {
setTimeout(function(){
resolve("Success!");
}, 250);
});
---
}
在这里我得到这个错误: - 无法未定义读取属性“然后”。
我是新来的节点和JS。
无极一个不工作,因为你还没有返回你创建的承诺,你也有'然后(set_file(),console.error)',临时工t会立即调用set_file,因为你有'()',它告诉它调用它而不是将它作为参考传递:'then(set_file,console.error)' –