1

I am a little confusing about the async function and sync function, how can I determine if it is an async function?

My assumption is all the function which accept a callback is non-block and async, but here is a exception I found:

I found the Array.prototype.forEach is a block function even if it accept a callback as a parameter.

function test(){
    [1,2,3,4,5].forEach(function(item){
        for(var i =0; i<100000; i++){
            console.log('test');
        }
    });
    console.log('end');
}

test();

this function will continue to print test until all the callback finish, it won't return at once to run console.log('end')

really confusing, how can I determine if a function will return at once?

Matthewgao
  • 281
  • 1
  • 4
  • 12
  • Look at documenation for Array. There are several functional methods which are synchronous. e.g. map, filter, reduce, sort etc. – chriskelly Jan 23 '16 at 16:36

1 Answers1

0

You can use the common sense rule here, is there any need for async in your code? Why do we need to delay the function? All of the array items are available for you, even if they were not, there should be another reader for example which is async then populate the array with avaialble items and then iterate over them. If you need async you will notice this for sure.

Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25