When using lodash chain to perform actions synchronously, I encountered an issue where .tap() is executed before the desired stage. I have been unable to find a solution using promises. I expected lodash chain to ensure actions are carried out in a synchronous manner, with tap only executing after forEach completes.
const ids = [
{
"id": 1,
"refs": [
{
"skuId": 693194,
"sizeId": "12M",
"colorId": "ROSE"
},
{
"skuId": 693195,
"sizeId": "14M",
"colorId": "ROSE"
},
{
"skuId": 973804,
"sizeId": "6M",
"colorId": "GREEN"
}
]
},
{
"id": 2,
"refs": [
{
"skuId": 693174,
"sizeId": "13M",
"colorId": "RED"
},
{
"skuId": 693995,
"sizeId": "14M",
"colorId": "BLUS"
}
]
}
]
let id = 1
_(ids)
.chain()
.map(value => {
id = _.result(_.find(value.refs, function(sku) {
return sku.colorId === 'ROSE' &&
sku.sizeId === '14M';
}), 'skuId');
})
.tap(() => console.log('id: ', id))
.value()
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>