同步调用函数的正确方式
Correct way to call function synchronously
首先让我说我知道 async/await 但我真的不想包括 babel,这似乎很麻烦而且我坚持承诺没有任何问题。所以我想做的很简单,
在我的函数中基本上实现了'synchronous'流程。
下面的代码给出了一个未处理的异常。我想听听任何想法,如果可能的话,我是否走在正确的轨道上。如果您有任何问题,请继续提问。
function A()
{
//...
result = B();
Promise.all(result).then(function(result){
//after finishing B continue
});
}
function B()
{
//..
C();
return number;
}
function C()
{
var data1;
var data2;
//..
calling_DB = DB_get(..., function(result){ data1 = ..;});//callback cause I ma fetching data from DB
Promise.all(data1).then(function(data1){
calling_DB2 = DB_get(..., function(result){ data2 = ..;});
Promise.all(data2).then(function(data2){
//...
});
});
}
您可以按照以下方法调用链中的那些函数
function A()
{
return B()
.then(function(_merged_db_get_results)
{
//after finishing B continue
console.dir(_merged_db_get_results);
return true;
})
.catch(function(error)
{
console.dir(error);
return false;
});
}
function B()
{
return C()
// Can be removed : START
.then(function(_merged_db_get_results)
{
return _merged_db_get_results;
});
// Can be removed : END
}
function C()
{
var db_1_res;
return Promise.resolve(true)
.then(function(_above_true)
{
return DB_get(condition);
})
.then(function(_db_get_results_1)
{
db_1_res = _db_get_results_1;
return DB_get(condition);
})
.then(function(_db_get_results_2)
{
return [db_1_res, _db_get_results_2];
});
}
首先让我说我知道 async/await 但我真的不想包括 babel,这似乎很麻烦而且我坚持承诺没有任何问题。所以我想做的很简单, 在我的函数中基本上实现了'synchronous'流程。
下面的代码给出了一个未处理的异常。我想听听任何想法,如果可能的话,我是否走在正确的轨道上。如果您有任何问题,请继续提问。
function A()
{
//...
result = B();
Promise.all(result).then(function(result){
//after finishing B continue
});
}
function B()
{
//..
C();
return number;
}
function C()
{
var data1;
var data2;
//..
calling_DB = DB_get(..., function(result){ data1 = ..;});//callback cause I ma fetching data from DB
Promise.all(data1).then(function(data1){
calling_DB2 = DB_get(..., function(result){ data2 = ..;});
Promise.all(data2).then(function(data2){
//...
});
});
}
您可以按照以下方法调用链中的那些函数
function A()
{
return B()
.then(function(_merged_db_get_results)
{
//after finishing B continue
console.dir(_merged_db_get_results);
return true;
})
.catch(function(error)
{
console.dir(error);
return false;
});
}
function B()
{
return C()
// Can be removed : START
.then(function(_merged_db_get_results)
{
return _merged_db_get_results;
});
// Can be removed : END
}
function C()
{
var db_1_res;
return Promise.resolve(true)
.then(function(_above_true)
{
return DB_get(condition);
})
.then(function(_db_get_results_1)
{
db_1_res = _db_get_results_1;
return DB_get(condition);
})
.then(function(_db_get_results_2)
{
return [db_1_res, _db_get_results_2];
});
}