gpt4 book ai didi

JavaScript Promise 绕过解析并继续执行 .then()

转载 作者:行者123 更新时间:2023-12-03 03:44:57 27 4
gpt4 key购买 nike

我在嵌套 Promise 方面遇到了一些问题,导致了忘记 Promise 问题。

let promiseList = new Promise((resolve, reject) => {
//first query to retrieve from firebase
query.once( 'value', data => {
var promises = [];

data.forEach(snapshot => {
//get item key

//second query based on item key
var promise = query.once('value');
promises.push(promise);

promise.then(data => {
var itemDetail = data.val();

var receiptID = itemDetail.receiptID;
// third query to find matching receiptID
var query = firebase.database().ref('receipts');
query.once('value', data => {
data.forEach(snapshot => {

snapshot.forEach(childSnapshot => {
if(childSnapshot.key == receiptID){
var branchDetail = childSnapshot.val().branch;
var branchName = branchDetail.branchName;

//console.log('inside promise ' + branchName);
datasetarr.push({branchName: branchName});
}
});

});
});

});
});

// wait till all promises are finished then resolve the result array
Promise.all(promises).then(() => resolve(datasetarr));
});
});

// print out array here
promiseList.then((arr) => {
for(var i = 0; i < arr.length; i++){
console.log(arr[i].branchName);
}
});

我设法用“inside Promise”从 console.log 打印出数据。但是,当我尝试从 .then() 打印它时,没有显示任何内容。

现在的问题是,在我解决 promise 之前,它实际上首先运行了 .then() 。

有什么想法吗?

最佳答案

我从未使用过 Firebase,但我确实知道 Promise。检查此示例链接 promise ,注意生成链接的 return 语句。

var outerPromise = query.once('value').then(data => {
// Promise array to group 2nd level promises and then do a Promise.all.
var promises = [];
// This will be the main output of the outerPromise.
// We will populate it asynchronously inside our 2nd level promises.
var datasetarr = [];
data.forEach(snapshot => {
// 2nd level promises, will be appended to the promises array.
// and will be enchained with the 3d level promise.
var promise = query.once('value').then(data => {
var itemDetail = data.val();
var receiptID = itemDetail.receiptID;
var query = firebase.database().ref('receipts');
// Third level promise. It's enchained by the return statement.
return query.once('value').then(data => {
data.forEach(snapshot => {
snapshot.forEach(childSnapshot => {
if(childSnapshot.key == receiptID){
var branchDetail = childSnapshot.val().branch;
var branchName = branchDetail.branchName;

//console.log('inside promise ' + branchName);
datasetarr.push({branchName: branchName});
}
});
});
});
});
promises.push(promise);
});

// We wait until 2nd (and third) level promises are ready
// and the return our desired output, the datasetarr
return Promise.all(promises).then(()=> datasetarr);
});

// Since it's all chained, the outerPromise will resolve once all promises are completed
// and we can get the output we supplied in the last chaining.
outerPromise.then((arr) => {
console.log(arr)
});

关于JavaScript Promise 绕过解析并继续执行 .then(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45439758/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com