React 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;
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以使用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);
    }