如何在子快照完成执行后调用函数



我想调用一个函数,一旦所有子快照都执行完毕,

进一步阐述:

我有一个数据集假设 -kvbd...具有两个子数据集的 SD

-KVBD...标清 ----------> -FDVDV-...JH ----------> -fgshs..嘘嘘

我想在对两个子节点进行处理后调用该函数

----------> -FDVDV-...JH ----------> -fgshs..嘘嘘

我的代码:

dRef
        .once("value")
            .then
              (
                function(snapshot)
                 {
                    snapshot.forEach(                                           
                    function(childsnapshot) 
                        {
                       var data=childsnapshot.val();
                       var Apdate=data.Dateval;                                       

                    for (i = 0; i < sel.options.length; i++)                        
                        {
                            var datein=sel.options[i].value;
                            console.log("check:"+datein);                                                        
                            if(datein==Apdate)                                                                                                                          
                            sel.options[i].disable = true;
                            console.log("Success:!!!!!!");                                                                                                              
                        }                                                       
                             pass();

                         }
                        }
                    }                       
                   );

你可以用Promise s尝试这样的事情:

ref.once('value')
  .then(function(snapshot) {
    var myFunctToRunAfter = function () {
      console.info('RUN AFTER THE LOOP')
    }
    return new Promise(function(resolve, reject) {
      snapshot.forEach(function(child) {
        console.log('I am a child ' + child.val())
      })
      /* Call the function after the loop */
      resolve(myFunctToRunAfter())
    })
    .then(function() {
      /* I could do something here */
      console.log(snapshot.val())
      console.info('RUN LAST')
    })
  })

这是一个工作示例:

var config = {
  apiKey: "AIzaSyCDyoPVmi2rLIFtb55eTGYYPMsxFR3uuMs",
  authDomain: "test-514b2.firebaseapp.com",
  databaseURL: "https://test-514b2.firebaseio.com",
  storageBucket: "test-514b2.appspot.com",
  messagingSenderId: "773249287825"
};
firebase.initializeApp(config);
var ref = firebase.database().ref();
ref.once('value')
  .then(function(snapshot) {
    var myFunctToRunAfter = function () {
      console.info('RUN AFTER THE LOOP');
    }
    
    return new Promise(function(resolve, reject) {
      snapshot.forEach(function(child) {
        console.log('I am a child ' + child.val())
      })
      /* Call the function after the loop */
      resolve(myFunctToRunAfter())
    })
    .then(() => {
      /* I could do something here */
      console.log(snapshot.val())
      console.info('RUN LAST')
    })
  })
<script src="https://www.gstatic.com/firebasejs/3.6.9/firebase.js"></script>

具有相同工作示例的代码笔。

相关内容

  • 没有找到相关文章