-1

I have this function that returns a list, but the list stays empty even after I add items to the list in the forEach. So I tried using Promises, but I need a little help. Right now the result is "undefined". How do I return the result list after the forEach loop is done?

async function return_list() {
    var result = [];

    var list_of_skus = [...];

    var promise = new Promise(() => {
        list_of_skus.forEach((number) => {

          api.get(...)
          //api request

          result.push(api_data)
        });
    });

    promise.then({
        return result;
    })
}

edit: i changed the code a bit: In the forEach loop im using an api request to get some data, and then add an item the result list

sander106
  • 51
  • 1
  • 7
  • Please add a [mre]. Promises isn't needed if you're not doing anything async. – 0stone0 May 17 '22 at 15:47
  • You're getting a syntax error, I'm pretty sure. You should probably fix that. – Pointy May 17 '22 at 15:48
  • Nothing here seems to be asynchronous. Moreover, `promise.then({ return result; })` should be throwing an error because it's invalid syntax. – VLAZ May 17 '22 at 15:49
  • `return_list()` doesn't return anything. – Barmar May 17 '22 at 15:49
  • Is that supposed to be `promise.then(() { return result; })`? That returns from the `.then()` callback, not from `return_list`. – Barmar May 17 '22 at 15:50
  • Adding a promise shouldn't make a difference unless you're calling an async function somewhere. Show the original version before you tried the promise. – Barmar May 17 '22 at 15:53
  • And if you're calling an async function in the real code, see https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Barmar May 17 '22 at 15:53

1 Answers1

0

Here you can find a bit more information about promises: Promises MDN

As long as you do nothing async (like network requests etc) you probably don't need promises in your forEach.

This should work:

function return_list() {
    const result = [];

    const list_of_skus = [...];

    list_of_skus.forEach((number) => {
        result.push(1)
    });

    return result;
}

If you need promises, this would work:

async function return_list() {
    const list_of_skus = [...];

    const promises = list_of_skus.map(async (number) => {
        return await somethingAsync(number)
    });

    return await Promise.all(promises)
}

As you transform every sku list item, you can use a map to transform all skus into a promise which does something async. Using Promise.all() it is possible to execute the promises in parallel.

Simolation
  • 64
  • 2
  • 4
  • Sorry I forgot to clarify in the code that i'm doing an api request and add that result to the result list. So based on your second solution, i make another function specifically for the api request? – sander106 May 18 '22 at 08:03
  • You could do that or do the API request inside the map body. Instead of `return await somethingAsync...`. It depends on how you do the request. If you use fetch or axios ect you can directly return the Promise they return. like `return fetch(...)`. If you do a regular XHR request, you would have to wrap that in a Promise. Then it would be easier to create a new function taking care of the request and returning a promise – Simolation May 19 '22 at 21:19