Is map() in javascript synchronous?

拟墨画扇 提交于 2019-11-28 12:01:18

Yes, map is synchronous.
It's a higher order function, that takes a new function and applies it to the given array.

Some people think that because they give a function as a parameter to map then it 'should' act like an event callback function, but it really doesn't. The map function just applies the function parameter to the array and only after it finishes, it continues execution for the resulting code after the map block.

As to your 'expected behavior' - it just doesn't work like you think ;)

"The map() method creates a new array with the results of calling a provided function on every element in this array."

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

The callback is called for each item, your logic is executed and the return value is set as an item in the new array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!