Reputation: 37
For some reason or another, my for of loop continues stopping after 1 iteration.
I'm expecting to get a console.log of 2 assetId's and 2 prices, however instead I'm only getting 1 assetId and 1 price. Then the only way I can get a response that suits is when I re-run the function via refresh (b/c it checks my localStorage and sees the old values are already there) any ideas why it's stopping after one iteration?
main = async () => {
await eth.connect({
provider: "https://mainnet.infura.io/[key-goes-here]",
contracts: [landRegistry, marketplace]
});
const response = await axios.get(
"https://api.decentraland.org/parcels?status=open"
);
const viableProperties = response.data.data.parcels.filter(parcel => parcel.publication.price <= this.state.landPrice);
for(let [key, value] of viableProperties.entries()) {
const cost = value.publication.price;
const asset = value.publication.asset_id;
const commaPos = asset.indexOf(",");
const coordinatesLat = parseFloat(asset.substring(0, commaPos));
const coordinatesLong = parseFloat(
asset.substring(commaPos + 1, asset.length)
);
const price = eth.utils.toWei(cost);
const oldProperty = localStorage.getItem(asset)
if (cost <= this.state.landPrice && oldProperty == null) {
localStorage.setItem(asset, key);
const assetId = await landRegistry.encodeTokenId(
coordinatesLat,
coordinatesLong
);
await marketplace.executeOrder(assetId, price)
}
}
};
**** EDIT **** - where I currently have await marketplace.executeOrder...I used to have console.log(asset, price)
Upvotes: 0
Views: 108
Reputation: 707616
So, now that you've shown us the real code with the:
await marketplace.executeOrder(assetId, price)
in it, there is a plausible reason for the for
loop to stop. If the promise that marketplace.executeOrder()
returns, rejects or if it throws a synchronous exception, then the async
function will immediately reject its promise and stop executing anything further.
If you want the loop to continue even if that promise fails, you can catch the rejected promise with either a try/catch
or a .catch()
.
Please take this as future advice to include as much of your real code as possible in the question. If you leave things out that you think aren't part of the issue, you may just be hiding the real issue from the people that are trying to help you (this happens a lot here and it really slows down our ability to help).
Upvotes: 1