비동기 호출과 약속을 처음 사용합니다. 나는 데이터베이스를 호출하는 세 가지 함수를 호출하려고합니다. 그 결과를 얻고 처리해야합니다. 나는 그것을 약속한다. 그것은 내가 올바른 길을 제시하지 말아야한다고 생각하고 있기 때문에 효과가 없습니다. 귀하의 모든 대답을 부탁드립니다. 나는 NODE JS에서 사용하고있다. 데이터베이스 함수가 3 개의 쿼리로 데이터베이스를 호출하는 것으로 보입니다.중첩 된 약속을 사용하여 함수를 호출하고 값을 가져 오는 방법
CODE
function nested_three_Promise_call_three_function(){
new Promise(function (resolve, reject) { return sample(); })
.then(
function (val) {
new Promise(function (resolve, reject) { sample1(); })
.then(
function (val) {
new Promise(function (resolve, reject) { sample2(); })
.then({
function(val) {
/* NEED TO GET VALUE OF ALL SAMPLE,SAMPLE1,SAMPLE 2 FOR FURTHER PROCESS BEFORE SEND TO RESPONSE*/
console.log("sample" + sample);
console.log("sample1" + sample1);
console.log("sample2" + sample2);
},
function(err){
console.log("error inside promise for sample2");
}
})
},
function (err) {
console.log("error inside promise for sample1");
}
)
},
function (error) {
console.log("error inside promise for sample1");
});
}
function sample() {
console.log("sample called");
database.query("select * from sample").then(function(result){
sample = result;
}).catch(function(error){
console.log("error occured in sample")
});
}
function sample1() {
console.log("sample called");
database.query("select * from sample1").then(function(result){
sample1 = result;
}).catch(function(error){
console.log("error occured in sample1")
});
}
function sample2() {
console.log("sample called");
database.query("select * from sample2").then(function(result){
sample2 = result;
}).catch(function(error){
console.log("error occured in sample2")
});
}
'database.query()'이미 약속을 반환하는 것 같다, 그래서 모두가 오히려 무의미한 것 같다 때 y ou는 그것을 반환 할 수 있고,'sample(). then (sample1) .then (sample2) .then (...' – adeneo
@adeneo 감사합니다. 이. – aka
질문에서 'new Promise (function (resolve, reject) {})'의'javascript '에서'resolve'가 호출되지 않는 것 같습니까? 'nested_three_Promise_call_three_function()'은 해결되거나 거부 된 약속 값이나 이유를 반환하지 않습니까? – guest271314