2017-11-05 20 views
0

트랜잭션을 업서 링하는 데 몽구스의 대량 작업을 사용하고 싶습니다. 각 트랜잭션에 대해 루프에서 루프를 처리하고 루프 내에서 약속을 사용해야합니다. 그 약속이 끝난 후, 나는 업서 트를 대량으로 추가하고 싶다.JavaScript - async/해결할 때까지 기다리지 않고 기다려야합니다.

내 모든 문제가 끝날 때까지 내가 await이긴하지만 약속이 해결되기 전에 함수의 끝에서 대량이 실행됩니다. 내가 잘못했거나 어떻게 해결할 수 있습니까?

const bulkTransactions = Transaction.collection.initializeUnorderedBulkOp(); 

    transactions.forEach(async (transaction: any) => { 

     // do some suff, fill transaction_data 

     await Utils.processTransactionType(transaction).then((action: any) => { 

      if (action) { 
       // do other stuff 
      } 

      bulkTransactions.find({_id: hash}).upsert().replaceOne(transaction_data); 

     }).catch((err: Error) => { 
      // log error 
     }); 
    }); 

    await bulkTransactions.execute().catch((err: Error) => { 
     // log error 
    }); 
+0

'transactions.forEach' 무엇입니까? 콜백에 의해 반환 된 약속을 어떻게 처리합니까? – Bergi

+0

트랜잭션은 트랜잭션 객체의 배열입니다. 나는''forEach'''로 배열을 가로 지른다. – phoebus

+0

글쎄 그게 문제이다 :-) – Bergi

답변

-1
await를 사용하는 경우

지금까지 내가 아는 한, 당신은 더 이상 then 반환 값을 사용하지 :

const bulkTransactions = Transaction.collection.initializeUnorderedBulkOp(); 

    transactions.forEach(async (transaction: any) => { 

     // do some suff, fill transaction_data 

     let action = await Utils.processTransactionType(transaction); 

     if (action) { 
      // do other stuff 
     } 

     bulkTransactions.find({_id: hash}).upsert().replaceOne(transaction_data); 


    }); 

    await bulkTransactions.execute().catch((err: Error) => { 
     // log error 
    }); 
+0

사실이지만 반드시 문제는 아니다. 그리고 이것은 문제를 해결하지 못합니다. – Bergi