Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Promissions for map函数在javascript中同步运行?_Javascript_Node.js_Sails.js - Fatal编程技术网

如何使用Promissions for map函数在javascript中同步运行?

如何使用Promissions for map函数在javascript中同步运行?,javascript,node.js,sails.js,Javascript,Node.js,Sails.js,我在Promise.all()方面遇到了一些问题。例如,如果finalArr有2个对象,则每行一次运行2次。它不是同步运行的: try{ let newData = await Promise.all(finalArr.map(async receiveData => { receiveData['internalCode'] = await RecievedLots.beforeRLCreation(receiveData.company_id)

我在
Promise.all()
方面遇到了一些问题。例如,如果finalArr有2个对象,则每行一次运行2次。它不是同步运行的:

try{
 let newData = await Promise.all(finalArr.map(async receiveData => {
          receiveData['internalCode'] = await RecievedLots.beforeRLCreation(receiveData.company_id)
          console.log(receiveData.internalCode)

           // For Example above console line is printing 2 times if finalArr has 2 objects
            // same like remaining functions.. how to avoid this?

          const createdReceiveMaterial = await RecievedLots.create(receiveData).fetch();

          if(!!createdReceiveMaterial && Object.keys(createdReceiveMaterial).length > 0) {
           const poMaterial = await POMaterials.findOne({id: receiveData.po_material_id});
            let status_id = poMaterial.status_id;
            let quantityReceived = poMaterial.qty_received + receiveData.qty_recieved
            let qtyAvailable = poMaterial.qty_available+ receiveData.qty_recieved;
             if(poMaterial.quantity <= quantityReceived){
               status_id = 6
             }
             else if(poMaterial.quantity > quantityReceived && quantityReceived != 0 ){
              status_id = 5
             }
             else if(quantityReceived == 0){
              status_id = 4
             }
          const updatePOmaterial = await POMaterials.update({id: receiveData.po_material_id})
         .set({qty_received:quantityReceived,status_id:status_id, qty_available: qtyAvailable}).fetch()
          // console.log(updatePOmaterial)
          }
         return receiveData
      }))

      cb(null, newData)
   }
   catch(err){
      cd(err)
   }
试试看{
让newData=wait Promise.all(finalArr.map)(异步接收数据=>{
receiveData['internalCode']=在创建之前等待ReceivedLots.beforeRLCreation(receiveData.company\u id)
console.log(receiveData.internalCode)
//例如,如果finalArr有2个对象,则上面的控制台行将打印2次
//与其他函数相同..如何避免这种情况?
const createdReceiveMaterial=wait receivedLots.create(receiveData.fetch();
如果(!!createdReceiveMaterial&&Object.keys(createdReceiveMaterial).length>0){
const poMaterial=wait POMaterials.findOne({id:receiveData.po_material_id});
让status_id=poMaterial.status_id;
let quantityReceived=poMaterial.qty\u received+receiveData.qty\u received
qtyAvailable=poMaterial.qty\u available+receiveData.qty\u received;
if(poMaterial.quantity接收的数量和接收的数量!=0){
状态id=5
}
否则如果(quantityReceived==0){
状态id=4
}
const updatepomaterials=wait POMaterials.update({id:receiveData.po_material_id})
.set({接收数量:接收数量,状态id:状态id,可用数量:qtyAvailable}).fetch()
//console.log(更新材料)
}
返回接收数据
}))
cb(空,新数据)
}
捕捉(错误){
cd(错误)
}

如果性能很重要且您不关心顺序,那么以“并行”方式解决提供的承诺实际上是
Promise.all()的优势之一。如果需要按顺序解析承诺,只需使用
for。。属于
-循环:

const newData = [];
for (const receiveData of finalArr) {
    receiveData['internalCode'] = await RecievedLots.beforeRLCreation(receiveData.company_id);
    // rest of your code here
    // ...
    // at the end simply push to newData instead of returning receive Data
    newData.push(receiveData);
}

如果性能很重要并且您不关心顺序,那么以“并行”方式解决提供的承诺实际上是
Promise.all()的优点之一。如果需要按顺序解析承诺,只需使用
for。。属于
-循环:

const newData = [];
for (const receiveData of finalArr) {
    receiveData['internalCode'] = await RecievedLots.beforeRLCreation(receiveData.company_id);
    // rest of your code here
    // ...
    // at the end simply push to newData instead of returning receive Data
    newData.push(receiveData);
}