Using JavaScript generators to optimize APIs