Debugging UI modification (issues with jQuery deferreds)

I have added a ‘bulk transplant’ feature and it’s nearly functional but I need help understanding some unexpected behaviour. Two things seem to be going wrong and I think it boils down to a lack of understanding of promises and deferred operation.

Basically I have two js functions. The first allows dragging a box and receives an object consisting of renewable resource nodes. The second is the placement function which is called with the object as an argument. It’s supposed to iterate over all the entities in the object and return only after the last one. At which point the first function calls itself so another box can be dragged if desired.

At first, it seemed like this was more or less happening, because while the first item would always fail to be “presented” for placement, the remaining objects were able to be placed. But as I looked further into it, the UI returns to the first function before the second is complete. I think this causes the failure of the first placement attempt and the failure to re-activate the selection tool after placement is complete.

Here’s a sample log where I selected three appropriate objects:

2016-08-21 22:54:32.327239 | client |  7 |                             mod stonehearth.bt_log.resource_call_handler | [] renewable: (196621 Wild Brightbell) [1]
2016-08-21 22:54:32.328239 | client |  7 |                             mod stonehearth.bt_log.resource_call_handler | [] renewable: (196774 Wild Brightbell) [2]
2016-08-21 22:54:32.328239 | client |  7 |                             mod stonehearth.bt_log.resource_call_handler | [] renewable: (196468 Wild Brightbell) [3]
2016-08-21 22:54:32.362239 |    cef |  3 |                          browser | console:(bt) bTR: b_t_r.done - item: object://game/196621
2016-08-21 22:54:32.362239 |    cef |  3 |                          browser | console:(bt) bTR: b_t_r.done - item: object://game/196774
2016-08-21 22:54:32.362239 |    cef |  3 |                          browser | console:(bt) bTR: b_t_r.done - item: object://game/196468
2016-08-21 22:54:32.362239 |    cef |  3 |                          browser | console:(bt) bTR: b_t_r.done - 3 items
2016-08-21 22:54:32.363239 |    cef |  3 |                          browser | console:(bt) pSI index: 0 item: object://game/196621
2016-08-21 22:54:32.365239 |    cef |  3 |                          browser | console:(bt) bTR returned from pSI
2016-08-21 22:54:32.398239 | client |  7 |                             mod stonehearth.bt_log.place_item_ch | [] choose_place_item_location: sess nil | resp [[deferred 1934 '[function fn:391 route:stonehearth:choose_place_item_location]']  state: initialized] | item (196621 Wild Brightbell) | trans nil
2016-08-21 22:54:32.405239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.fail - recurse
2016-08-21 22:54:32.406239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.always
2016-08-21 22:54:32.465239 |    cef |  3 |                          browser | console:(bt) pSI index: 1 item: object://game/196774
2016-08-21 22:54:32.466239 |    cef |  3 |                          browser | console:(bt) bTR: b_t_r.fail
2016-08-21 22:54:32.472239 | client |  7 |                             mod stonehearth.bt_log.place_item_ch | [] choose_place_item_location: sess nil | resp [[deferred 1951 '[function fn:397 route:stonehearth:choose_place_item_location]']  state: initialized] | item (196774 Wild Brightbell) | trans nil
2016-08-21 22:54:33.524239 | server |  7 |                             mod stonehearth.bt_log.place_item_ch | [] get_root_entity: (196774 Wild Brightbell) table: 00000000C00EE530
2016-08-21 22:54:33.525239 | server |  7 |                             mod stonehearth.bt_log.place_item_ch | [] place_item_in_world: loc (135.00, 40.00, -23.00) | normal (0.00, 1.00, 0.00) | item (196774 Wild Brightbell) | entity_forms table: 00000000C00EE530
2016-08-21 22:54:33.541239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.done - recurse
2016-08-21 22:54:33.541239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.always
2016-08-21 22:54:33.593239 |    cef |  3 |                          browser | console:(bt) pSI index: 2 item: object://game/196468
2016-08-21 22:54:33.608239 | client |  7 |                             mod stonehearth.bt_log.place_item_ch | [] choose_place_item_location: sess nil | resp [[deferred 1976 '[function fn:407 route:stonehearth:choose_place_item_location]']  state: initialized] | item (196468 Wild Brightbell) | trans nil
2016-08-21 22:54:34.382239 | server |  7 |                             mod stonehearth.bt_log.place_item_ch | [] get_root_entity: (196468 Wild Brightbell) table: 00000000C041AF40
2016-08-21 22:54:34.382239 | server |  7 |                             mod stonehearth.bt_log.place_item_ch | [] place_item_in_world: loc (133.00, 40.00, -29.00) | normal (0.00, 1.00, 0.00) | item (196468 Wild Brightbell) | entity_forms table: 00000000C041AF40
2016-08-21 22:54:34.411239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.done - no more items
2016-08-21 22:54:34.413239 |    cef |  3 |                          browser | console:(bt) pSI: c_p_i_l.always

(Note: I added logging to place_item_call_handler.lua but it’s otherwise unmodified. I also enabled logging of the _callTool wrapper function but parsed it out here.)

Full source
Aside from a custom icon, all the files are modified versions of resources from stonehearth.smod. I guess anyone who would be able to help me knows what they need to do with it.
The archive contains my .git folder. The current state isn’t committed and the commits only contain logging as I made my way to this point. The last couple might help understand my thought process if it’s not clear.

Thanks for reading.