title: How can I return the value of a deferred method in javascript
body: <p>I am trying to write a module which projects coordinates into a new Spatial Reference, but the javascript syntax has beaten me once again.</p>
  
  <p>This is how the module currently looks like:</p>
  
  <pre><code>define(["esri/geometry/Point", ...], 
      function(Point, SpatialReference, ...){
  
      var gmsvc = new GeometryService("http://server/maps/rest/services/Utilities/Geometry/GeometryServer");
      /*...*/
  
      return {
          /*...*/
          //transforms the current point on the map to the new wkid
          transformCoordinates: function(point, newWkId) {
  
              var param = new ProjectParameters();
              var newPoint;
              param.geometries = [point];
              param.outSR = new SpatialReference({ wkid: newWkId});
  
              gmsvc.project(param);
  
              gmsvc.on('project-complete', function(result) {
                  newPoint = result.geometries[0];
              });
  
              return newPoint;
          }   
      };
  });
  </code></pre>
  
  <p>As per the <a href="https://developers.arcgis.com/javascript/jsapi/geometryservice.html#project" rel="nofollow">documentation</a> <code>GeometryService.project(parameter)</code> is marked as deferred, so I try to access the result of it in the OnProjectComplete event. At this point <code>result.geometries[0]</code> is a valid Point with an x, y and SpatialReference property.
  Assigning this to the <code>newPoint</code> variable and returning it, results in an <code>undefined</code> being returned:</p>
  
  <pre><code>console.log(CoordinateTransUtils.transformCoordinates(evt.mapPoint, 5254));
  &gt; undefined
  </code></pre>
  
  <p>Changing the code to</p>
  
  <pre><code>return gmsvc.on('project-complete', function(result) {
      return result.geometries[0];
  });
  </code></pre>
  
  <p>results in a return value of:</p>
  
  <pre><code>Object {id: 216, receiveArguments: true, previous: Object}advice: (){var b,c={},e=arguments.length;for(b=0;b&lt;e;b++)c[f[b]]=arguments[b];c.target||(c.target=d);a.call(d,c)}id: 216next: Objectprevious: ObjectreceiveArguments: trueremove: (){if(h.advice){var c=h.previous,d=h.next;!d&amp;&amp;!c?delete b[e]:(c?c.next=d:b[e]=d,d&amp;&amp;(d.previous=c));b=a=h.advice=null}}__proto__: Object
  </code></pre>
  
  <p>which isn't a point either.
  My question therefore is, how to return the point stored in <code>result.geometries[0]</code> with an explanation what I am doing wrong here.</p>