Modify then function to wait till the queue has drained completely