【问题标题】:React Native - A function does not wait another function returnReact Native - 一个函数不等待另一个函数返回
【发布时间】:2018-03-26 17:40:32
【问题描述】:

我正在尝试学习 react-native 并且我创建了基本的 react-native 项目。我的问题是一个函数不等待另一个函数返回值。 如下我有两个函数,prepareSupplierStockPath() 和addSupplierStock()。我在“addSupplierStock”中调用了“prepareSupplierStockPath”。我想先“prepareSupplierStockPath”返回,然后“addSupplierStock”继续。 我尝试了.then(),但出现错误:“.then() 不是函数” 我还尝试了 async/await,subscribe() vs. 但不起作用。我该怎么做?

感谢您的帮助,对不起我的英语不好。

export function addSupplierStock(supplier, stock) {
    var pathList = prepareSupplierStockPath(supplier);
    return (dispatch) => {
        dispatch({type: ADD_SUPPLIER_STOCK})
            firebase.database().ref('stock')
                .orderByKey()
                .equalTo(stock)
                .on('value', snapshot => {
                    for(var i; i<pathList.length; i++){
                        firebase.database().ref(pathList[i])
                            .push({stock: snapshot.val()})
                            .then(supplierStock => addSupplierStockSuccess(dispatch, supplierStock))
                            .catch(() => addSupplierStockFail(dispatch));
                    };
                });
    };
}

 export const prepareSupplierStockPath = (supplier) => {
         var pathList = [];
         firebase.database().ref(`market_suppliers`)
             .on('value', snapshot => {
                 for (var key1 in snapshot.val()) {
                     firebase.database().ref(`market_suppliers/${key1}/market`)
                         .on('value', snapshot => {
                             for (var key2 in snapshot.val()) {
                                 firebase.database().ref(`market_suppliers/${key1}/market/${key2}`)
                                     .on('value', snapshot => {
                                         for (var key3 in snapshot.val()) {
                                             firebase.database().ref(`market_suppliers/${key1}/market/${key2}/${key3}/supplier`)
                                                 .orderByKey()
                                                 .equalTo(supplier)
                                                 .on('value', snapshot => {
                                                     for (var key4 in snapshot.val()) {
                                                         debugger;
                                                         pathList.push('market_suppliers/' + key1 + '/market/' + key2 + '/' + key3 + '/supplier/' + key4 + '/stock');
                                                     }
                                                 });
                                         }
                                     });

                             }
                         });
                 }

             });
     return pathList;
}

【问题讨论】:

    标签: react-native


    【解决方案1】:

    在这种情况下,您可以使用 javascript 回调函数。

       export function addSupplierStock(supplier, stock) {
          prepareSupplierStockPath(supplier, (pathList, dispatch) => {
             dispatch({type: ADD_SUPPLIER_STOCK})
                firebase.database().ref('stock')
                .orderByKey()
                .equalTo(stock)
                .on('value', snapshot => {
                    for(var i; i<pathList.length; i++) {
                        firebase.database().ref(pathList[i])
                            .push({stock: snapshot.val()})
                            .then(supplierStock => addSupplierStockSuccess(dispatch, supplierStock))
                            .catch(() => addSupplierStockFail(dispatch));
                    };
                });
             }); 
          };
       }
    

    然后在您的 prepareSupplierStockPath 函数中

        export const prepareSupplierStockPath = (supplier, callback) => {
              ...... your code here .....
              return callback(pathList);
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多