Download CSV file to the client
Im more familiar with node than ajax ( i had the same issue in nodejs which i fixed here on stackoverflow), but maybe this will help:
$.ajax({ async: false,
from How do I make jQuery wait for an Ajax call to finish before it returns?