PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

[FIXED] how to use optional chaining with map operator?

 October 20, 2022     node.js, optional-chaining     No comments   

Issue

I have a scenario where I am reading a yaml file and based upon the data, make calls to tag aws resources.

await Promise.all(doc?.SQS.map(async (queue)=>{
  // code to tag 1 queue
}))

Now the yaml file may not have a any sqs, in which case doc?.SQS returns undefined as expected. How can I make the map to not run in this scenario ? I tried below but it didn't work as expected.

await Promise.all(doc?.SQS?.map?.(async (queue)=>{
  // code to tag 1 queue
}))

.

await Promise.all(doc?.SQS && doc?.SQS?.map(async (queue)=>{
  // code to tag 1 queue
}))

Solution

doc?.SQS?.map() won't run if either doc or SQS is undefined or null. That's the purpose of conditional chaining.

The problem is, that Promise.all expects an iterable. But if doc.SQS is not defined doc?.SQS?.map(...) results in undefined, which is not iterable. Thus Promise.all throws an error.

So you can either check before calling Promise.all

if (doc?.SQS) {
  await Promise.all(doc.SQS.map(...))
}

or you can ensure, that Promise.all always receives an iterable -- even if it's just an empty array

await Promise.all(doc?.SQS?.map(...) || []);

or

await Promise.all((doc?.SQS? || []).map(...));

I personally would opt for the first way, because it seems much more readable, especially if the callback of map is more than a oneliner ...



Answered By - derpirscher
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing