JavaScript承诺不与resolve

JavaScript承诺不与resolve

本文介绍了JavaScript承诺不与resolve()一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中遇到了一些问题。所以我想做的是我想从firebase中检索,然后将所有返回的结果存储到数组中。之后,我将对数组执行一些排序。这里是我的代码:

pre $ let promise = new Promise((resolve,reject)=> {
var query = firebase.database()。ref('');
query.once('value',data => {
data.forEach(subtypeSnapshot => {
var itemData = ;

var query = firebase.database()。ref('')。child(itemKey);
query.once('value',data => {
var itemDetail =;
datasetarr.push();
});
});
resolve(datasetarr);
});
}) ;

使用这组代码,从第一个 console.log 里面的承诺,我设法得到这些在我的控制台:

这意味着我的firebase检索没有任何问题。在这之后,我想把它们存储到数组中,这就是这个部分:

$ $ $ $ $ $ $> datasetarr.push({type:subtype,数量:数量});

完成所有工作后,我解决了承诺,承诺完成后,阵列。但是,在 .then()内的for循环中没有任何东西被打印出来。任何想法?

解决方案

正如已经提到:您的承诺太早解决。



您可以使用



根据, query.once('value')返回一个Promise, 。
$ b 编辑:像这样

  var datasetarr = []; 
let promiseItemDataList = new Promise((resolve,reject)=> {
var query = firebase.database()。ref('receiptItemIDsByCategory')。child(category);
query。一次('value',data => {
var promises = []; // NEW LINE

data.forEach(subtypeSnapshot => {
var itemData = subtypeSnapshot .val();
var itemKey = subtypeSnapshot.key;

var query = firebase.database()。ref('receiptItems')。child(itemKey);
var promise = query.once('value'); // NEW LINE
promises.push(promise); // NEW LINE

promise.then(data => {//。然后代替回调
var itemDetail = data.val();
var subtype = itemDetail.type;
var quantity = itemDetail.quantity;
console.log('inside promise'+ subtype +''+ quantity);
datasetarr.push({type:子类型,数量:数量});
});
}); $(b)b
$ b Promise.all(promises).then(()=> resolve(datasetarr)); // NEW LINE
});
}); ((arr)=> {
for(var i = 0; i< arr.length; i ++){
console.log('outside promise'+ arr [i] .type +''+ arr [i] .quantity);
}
});


I was having some problem with promise in JavaScript. So what I am trying to do is I wanted to retrieve from firebase, then store all the result returned into array. After that I will perform some sorting on the array. Here is my code:

    let promise = new Promise((resolve, reject) => {
    var query = firebase.database().ref('');
        query.once( 'value', data => {
            data.forEach(subtypeSnapshot => {
                var itemData = ;

                var query = firebase.database().ref('').child(itemKey);
                query.once( 'value', data => {
                    var itemDetail = ;
                    datasetarr.push();
                });
            });
            resolve(datasetarr);
        });
    });

With this set of code, from the first console.log inside the promise, I managed to get these in my console:

With these, it means there is nothing wrong with my firebase retrieval. Afterwhich, I wanted to store each of them into array and this is the part:

datasetarr.push({type: subtype, quantity: quantity});

After done everything and I resolved the promise, when promise is done, I then print out the items in array. However, nothing is printed out at the for loop inside .then(). Any ideas?

解决方案

As already mentioned: Your Promise is resolved too early.

You can use Promise.all to wait for all promises to resolve before resolving the wrapping Promise. I put together a simple example, however, because of the lack of a firebase database, I just use functions returning Promises: https://jsfiddle.net/57b0gkLt/

According to the firebase documentation, query.once('value') returns a Promise, so this should work.

EDIT: Like this

var datasetarr = [];
let promiseItemDataList = new Promise((resolve, reject) => {
var query = firebase.database().ref('receiptItemIDsByCategory').child(category);
    query.once( 'value', data => {
        var promises = []; // NEW LINE

        data.forEach(subtypeSnapshot => {
            var itemData = subtypeSnapshot.val();
            var itemKey = subtypeSnapshot.key;

            var query = firebase.database().ref('receiptItems').child(itemKey);
            var promise = query.once('value'); // NEW LINE
            promises.push(promise); // NEW LINE

            promise.then(data => { // .then instead of a callback
                var itemDetail = data.val();
                var subtype = itemDetail.type;
                var quantity = itemDetail.quantity;
                console.log('inside promise ' + subtype + ' ' + quantity);
                datasetarr.push({type: subtype, quantity: quantity});
            });
        });

        Promise.all(promises).then(() => resolve(datasetarr)); // NEW LINE
    });
});

promiseItemDataList.then((arr) => {
    for(var i = 0; i < arr.length; i++){
        console.log('outside promise ' + arr[i].type + ' ' + arr[i].quantity);
    }
});

这篇关于JavaScript承诺不与resolve()一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:00