A wiser way to write async javascript