私は非同期呼び出しと約束を新しくしました。私はデータベースを呼び出す3つの関数を呼び出すしたい。私はそれらの結果を得てそれを処理する必要があります。私はそれを約束します。私は正しいことをしてはならないと思っているので、それはうまくいかない。あなたの答えは高く評価されます。私は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 (sample1).then(sample2).then(... ' – adeneo
@adeneo私はあなたが本当にすべての人に新しい時代になって少しずつ喜んでくれてありがとうと思いますか?この。 – aka
質問で 'new Promise(function(resolve、reject){}) 'の' javascript'で 'resolve'が呼び出されていないようですか? 'nested_three_Promise_call_three_function()'は解決または拒否された約束の値または理由を返しませんか? – guest271314