私は2つの異なるサービスにログインしており、両方の応答を配列にプッシュする必要があります。そのために、私はpromiseResult
、そしてその中に、ログイン用の2つの他の約束作成しています:私はコメントで書いたようにNodeJS:約束内のコールバック
var promiseResult = new Promise(function(resolveResult, rejectResult) {
var dataAvailable = []
// first promise for the first login
var promiseFirstLogin = new Promise(function(resolve, reject) {
login.returnData(email, password, (dataFirstLogin) => {
resolve(dataFirstLogin)
})
})
promiseFirstLogin.then(function(dataFirstLogin) {
return dataFirstLogin
})
.then(function(dataFirstLogin) {
// pushing the data of the first login
dataAvailable.push({dataFirstLogin: dataFirstLogin})
return dataAvailable
})
// if the user puts the login for the second service
if (second_login_username) {
// second promise of the second login
var promiseSecondLogin = new Promise(function(resolve, reject) {
login.returnSecondData(secondUsername, secondPassword, (secondData) => {
resolve(secondData)
})
})
promiseSecondLogin.then(function(secondData) {
return secondData
})
.then(function(secondData) {
// pushing second data to the same array
dataAvailable.push({secondData: secondData})
return dataAvailable
})
}
// logs undefined (?)
console.log('->', dataAvailable);
/*
I try to resolve the array with my data, but it needs to be inside the promises.
However, as I have multiple data sources, I cannot simply put the resolve function
inside each promise. How to proceed with this?
*/
resolveResult(dataAvailable)
})
promiseResult.then(function (dataAvailable) {
// I try to get the array with my data... but unsuccessfully
return dataAvailable
})
.then(function (dataAvailable) {
dataAvailable.reduce(function(result, item) {
var key = Object.keys(item)[0]
result[key] = item[key];
res.send(JSON.stringify(result, null, 3));
}, {})
})
を、私は両方のログインからのデータを持つ配列をresolve()
しようとするが、それは必要約束の中にいること。しかし、私は複数のデータソースを持っており、それぞれの約束の中に単にresolve()
を置くことはできません。私のサービスの両方のデータを含む単一のresolve()
を入れるには?
ご協力いただければ幸いです。
を使用しています。私の質問をどのように改善できるか教えてください。 –
私の答えを確認しましたか?それがあなたがする必要があることです。 –
はい、私はしました - 私は本当にそれを感謝しupvoted。 –