c# – Adding some wait time between API requests to avoid overloading the server


In Shopify a product can belong to a collection.

I have written a method which accepts an organisation and list of collectionIds… I need to loop through collectionIds and get the products associated with each one (unfortunately I cannot pass a list of collections to get the response at once)

I don’t want to overload Shopify server by sending too many requests, so I have added 75 ms wait between each request. But since this method is async I am not quite sure if adding Thread.Sleep is correct way adding wait between each request?

public async Task<List<Collection>> GetAllCollections(Organisation organisation, List<long> collectionIds)
{
    int waitMilliSeconds = 75; // wait 75 ms between each request
    List<Collection> allCollections = new List<Collection>();
    var collectionService = new CollectionService(organisation.Website, organisation.AccessToken);

    foreach (var collectionId in collectionIds)
    {
        var collection = await collectionService.GetAsync(collectionId);
        Thread.Sleep(waitMilliSeconds);
        allCollections.Add(collection);
    }

    return allCollections;
}