JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – what does the async array map function return

JavaScript Quiz – what does the async array map function return

Asycn Array Map Method

In javascript, the async array map method is a map method that uses async before the map function. A simple array map method modifies the object based on condition.

But async array map method return the promise object instead of modified value. So, you need to pass async map array to promise.all to give the same result as map method.

Async Array Map Syntax Be Like –

array.map( async function(currentValue, index, array), thisValue)

If you want to know more about the javascript array map method click here.

JavaScript Question

In this javascript quiz, find out what does the async array map function return because map method modify the array values based on condition.

First, we declare variable numbers using the const keyword and assign an javascript array which contain only numbers.

const numbers = [1, 3, 5];

In the second step, we create an function with name addOne and add async to it.

const addOne = async (num) => num + 1;

Now, create another variable numbersPlusOne, and assign the array return from the map method.

const numbersPlusOne = numbers.map(addOne);

Finally, we are checking the value numbersPlusOne using console log.

JavaScript Quiz Test

const numbers = [1, 3, 5];
const addOne = async (num) => num + 1;
const numbersPlusOne = numbers.map(addOne);

console.log(numbersPlusOne); // output => ?

Any idea what should be the output of above code snippet ?

Answer – [Promise, Promise, Promise]

Click To Run Code

Explanation

Now, you know the answer is [Promise, Promise, Promise] but why doesn’t it should provide the array with [2, 4, 6]. Let me explain.

In short, the answer is [Promise, Promise, Promise] because the async function returns the promise object. That’s why each element converted a promise object.

If you want to get the same result as map method then you have pass the result array to Promise.all(array)

At last, That’s why we get an answer “[Promise, Promise, Promise]“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – numbers array can be sort
  2. JavaScript Quiz – switch case can change a variable value
  3. JS Quiz – zero date can give get full year method
  4. JS Quiz – does set object really store duplicate object

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link