$.getJSON example on callback function

$.getJSON example on callback function

I want to access data from a $.getJSON call, and return it as an array. My data is transferred correctly to my callback function, but is still undefined when I call my function. Where did I get it wrong? Can anyone please show me an example using $.getJSON?

My code:

  1.     function countTypes( resource ) {
  2.         $.getJSON( resource, otherFunction ); 
  3.     }

  4.     function otherFunction( data ) {
  5.         var types = [];
  6.         $.each(data, function( i, item ) {
  7.             types.push( item['id'] );
  8.         });
  9.         console.log( types ); // This one works :)
  10.         return types;
  11.     }

  12.     types = countTypes( 'my_resource' );  // types is undefined :(