Question: I'm using `ng-repeat` to create an index of records within a Rails view, on this index I'm trying to implement a button to update a particular record. The problem is I need to pass the correct id through to the Rails controller. I'm getting an error when I attempt to pass the record id through with `'{{swim_record.id}}!'` , using string interpolation via Angular.

```
<tbody>
  <tr ng-repeat='swim_record in SwimRecords | orderBy:predicate.value:reverse |
  filter:search'>
   <td>
     <a ng-href='/swim_records/{{swim_record.id}}'>
      {{swim_record.last_name}}
     </a>
   </td>
   <td>{{swim_record.first_name}}</td>
   <td class = 'hidden-xs'>{{swim_record.check_in | date:'MM/dd/yyyy @ h:mma'}}</td>
   <th class = 'hidden-xs'>{{swim_record.lmsc}}</td>
   <td>
    <%= bootstrap_form_for Swimmer.find_by_id("{{swim_record.id }}!".to_i) do |f| %>
     <%= f.hidden_field :last_name, value: "Hat" %>
     <%= f.submit 'Check Out', class: "btn btn-danger" %>
    <% end %>
   </td>
  </tr>
</tbody>

```

Any suggestions would be very much appreciated!

Answer: 
> 
> Why is my function returning before the X,Y variables are set?
> 
> 
> 

[Because JavaScript I/O is asynchronous](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call). 

If you want to wait for two promises - you need to hook on the promise's completion. Luckily, promises make this super simple for you with `Promise.all` and `Promise.spread`.

```
Promise.all(getX(rows[0].id,con,mysql),getY(rows[0].id,con,mysql)).spread(function(x,y){
     console.log(x,y);//should work;
});

```
The functions `getX` and `getY` are asynchronous functions. Promise solves the issue of nesting anonymous functions, but it don't make the functions synchronous and blocking.

So `x` and `y` are not set at the moment you create the `ref` object and return this object.

Try something like this:

```
getX(rows[0].id,con,mysql).then(function(data) {
    x = data; //x logs the return 7 from the db
    getY(rows[0].id,con,mysql).then(function(data) {
        y = data; //y logs 45 from the db
        console.log(x,y,"line77"); //logs "undefined undefined line77"
        ref = new P(rows[0].id,x,y);
    });
});

```

Also because your whole function is asynchronous, you have to work with an callback or return a promise, in which you have access to `ref` Object.