public async Task <PagingResultObject> GetLatestNews(int pageNum, int pageSize) { try { if (pageNum < 1) { pageNum = 0; } //this is a purposefully inconsisten handling of validation. see assumptions.component.html for details if (pageSize < 1 || pageSize > 100) { //Ideally this would be returning a proper HTTP response code...such as 422 throw new ValidationException("Page size must be greater than or equal to 1 and cannot exceed 100"); } //if an error occurs, we can log it and return an empty array. //or, if hackerNews is down, perhaps we should return an error response 500 or something int[] latestNewsIdsArray = await _service.GetIdsAsync(); PagingResultObject pageResult; if (latestNewsIdsArray == null || latestNewsIdsArray.Length == 0) { //presumably this is a valid scenario and so we're not going to return an error response. Just make sure the caller handles this. pageResult = new PagingResultObject { PageSize = pageSize, PageNum = 0, NumItemsTotal = 0, NumPagesTotal = 0 }; } else { int numPages = this.CalculateNumPages(latestNewsIdsArray, pageSize); int actualPageNum = this.CalculateRealPageNumber(latestNewsIdsArray, pageNum, pageSize); int pageStartIndex = this.CalculatePageStartIndex(latestNewsIdsArray, actualPageNum, pageSize); int pageEndIndex = this.CalculatePageEndIndex(latestNewsIdsArray, actualPageNum, pageSize); pageResult = new PagingResultObject { PageSize = pageSize, PageNum = actualPageNum, NumItemsTotal = latestNewsIdsArray.Length, NumPagesTotal = numPages, PageStartItemNum = pageStartIndex + 1, PageEndItemNum = pageEndIndex + 1, PageIds = latestNewsIdsArray.Skip(pageStartIndex).Take(pageEndIndex - pageStartIndex).ToArray() }; //some sort of c# map => Promise => promise.all would be better here List <NewsArticle> pageArticles = new List <NewsArticle>(); for (int i = 0; i < pageResult.PageIds.Length; i++) { int articleId = pageResult.PageIds[i]; NewsArticle article = await _service.GetItemByIdAsync(articleId); //I have seen some Ids come back with null responses. I have double-checked by hitting their api directly. //seems like overkill to prevent the page from loading because one of the items has an issue, //so we'll just create a dummy entry and make sure the caller handles it.. if (article == null) { article = new NewsArticle { Id = articleId, Title = "<Error, Item not found>", By = "?", Url = null }; } pageArticles.Add(article); } pageResult.PageItems = pageArticles.ToArray(); } return(pageResult); } catch (Exception ex) { throw; } }