顺序解决承诺不起作用

时间:2019-05-15 11:28:04

标签: javascript promise es6-promise

我有一个代码可以用来

  • 从Firebase读取最后三个数据
  • 迭代每个检索到的数据
  • 将Promise返回函数表达式推入Promise数组中,以便稍后顺序处理
  • 依次处理所述数组

代码:

firebase.database().ref('someRef').limitToLast(3).on('value', snapshot => {
  let promiseArray = [];
  
  snapshot.forEach(e => {
    promiseArray.push(() => {
      firebase.storage().ref(e.key).getDownloadURL().then(url => {
        //Do something with URL
        //In this case, I print out the url to see the order of URL retrieved
        //Unfortunately, the order was incorrect
        return 'Resolved, please continue'; //Return something to resolve my Promise
      });
    });
  });
  
  let result = Promise.resolve([]);
  promiseArray.forEach(promise => {
    result = result.then(promise);
  });
});

我认为应该已经是正确的。但是,我想要得到的结果是错误的。我想念什么?

编辑

我似乎漏了一点。 在我的Promise数组中,我希望第一个函数先解析它返回的Promise,然后再继续执行第二个函数。

3 个答案:

答案 0 :(得分:0)

forEach是一种同步方法。您可以使用map创建承诺数组,然后使用promise.all

firebase.database().ref('someRef').limitToLast(3).on('value', snapshot => {
  let promiseArray = [];

  const promiseArray = snapshot.map(e => firebase.storage().ref(e.key).getDownloadURL());

  Promise.all(promiseArray).then((resultArr) => {
    // Do anything with your result array
  });
} 

要依次执行承诺,可以使用async await

firebase.database().ref('someRef').limitToLast(3).on('value', async (snapshot) => {
    let promiseArray = [];

    const promiseArray = snapshot.map(e => firebase.storage().ref(e.key).getDownloadURL());
    let result;
    for(let i = 0; i < promiseArray.length; i++) {
        result = await promiseArray[i]; 
    }
});

答案 1 :(得分:0)

您应该使用system( "sudo echo 'statsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-126.78,i1=9.07,v2=341.16,i2=2.67,v3=-204.78,i3=-6.32 1557916955797397000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-171.62,i1=8.56,v2=347.97,i2=1.35,v3=-157.54,i3=-7.16 1557916955797898000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-213.26,i1=8.03,v2=345.71,i2=0.03,v3=-106.45,i3=-7.91 1557916955798398000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-249.82,i1=7.33,v2=333.84,i2=-1.25,v3=-54.12,i3=-8.55 1557916955798898000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-281.72,i1=6.56,v2=313.43,i2=-2.47,v3=-2.24,i3=-8.85 1557916955799399000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-329.42,i1=4.38,v2=257.89,i2=-4.66,v3=90.23,i3=-8.7 1557916955800269000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-343.94,i1=2.91,v2=214.3,i2=-5.69,v3=138.46,i3=-8.23 1557916955800769000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-350.6,i1=1.5,v2=165.64,i2=-6.5,v3=183.03,i3=-7.65 1557916955801270000\nstatsd_vmart_4d,company=prop-new,machine=EdgeMotor1,machineId=machine-1ad271a0-d6a4-11e8-8f3a-433749296f72,subassemblyinstance=motorSense1,datatype=eventtrap,collector=pf1,param=rawvalues v1=-347.9,i1=-0.02,v2=114.14,i2=-7.37,v3=223.18,i3=-6.99 1557916955801771000\n'" ) 。您将在这里找到一个很好的示例:https://decembersoft.com/posts/promises-in-serial-with-array-reduce/

答案 2 :(得分:0)

我发现了:显然,我忘记了我的函数没有返回Promise。因此,当我链接then时,它并不是在等待我的Promise首先解决,因为它甚至根本没有返回。我基本上是返回一个void,因此这些函数可以继续运行而无需等待先前的Promise解决。只需添加return即可解决问题:

firebase.database().ref('someRef').limitToLast(3).on('value', snapshot => {
  let promiseArray = [];
  
  snapshot.forEach(e => {
    promiseArray.push(() => {
      return firebase.storage().ref(e.key).getDownloadURL().then(url => { //Add return here
        //Do something with URL
        //In this case, I print out the url to see the order of URL retrieved
        //Unfortunately, the order was incorrect
        return 'Resolved, please continue'; //Return something to resolve my Promise
      });
    });
  });
  
  let result = Promise.resolve([]);
  promiseArray.forEach(promise => {
    result = result.then(promise);
  });
});