6

I'm having problems with some of my code specifically this part. I'm calling an async method that returns a Task<List<>> (cant return a regular list because the method is async). But, I'm not able to convert it to a regular list, which is what I need. Is that even possible? If so, is anyone able to help out? :)

static void Main(string[] args) {
    Program p = new Program();

    List<Product> products = p.getProducts();
}

public async Task<List<Product>> getProducts() {
    // calling woocommerce api
    MyRestApi rest = new MyRestApi("http://someurl.com/wp-json/wc/v2", "consumer key", "consumer secret");
    WCObject wc = new WCObject(rest);

    List<Product> products = await wc.Product.GetAll();

    return await Task.Run(() => new List<Product>(products));
}
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
  • See here: https://stackoverflow.com/questions/25720977/return-list-from-async-await-method – Flot2011 Jul 13 '17 at 09:52
  • Unlike the duplicate, this question is actually containing the answer (just not at the place where the author wants it). `List products = await wc.Product.GetAll();` is extracting the list from the `Task>` that was returned by `wc.Product.GetAll()`. Probably the best help is a way to async-await in the code that is currently the main method. https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app – grek40 Jul 13 '17 at 11:53

0 Answers0