Promise.all not working in backend code

The below function “promisall” that uses Promise.all works perfectly on frontend code in a browser but put it in backend code and it doesnt wait for the promises to be fulfilled. If am correct and it doesnt work in backend code, any workarounds or have i messed up.

export function promiseall() {
let fcaarray = ;
getarray().then((result) => {
for ( var i = 0; i < result.items.length; i++) {
fcaarray[i] = getfcainfo(result.items[i].frn);
}
Promise.all(fcaarray).then(info => {
console.log(“Here”)})
});
}

export function getfcainfo(fcanumber) {
const url = ‘URL’ + fcanumber;
return fetch(url, {
method: “get”,
headers: {
‘Content-Type’: ‘application/json’
},
})
.then(response => response.json())
}

So we can take it that you are using asynchronous code, so please refer to this page about working with promises in Wix Corvid if you have not already seen it.
https://support.wix.com/en/article/corvid-working-with-promises

Also, as you are using Wix Fetch, check that your code is correct there for working within the backend too.
https://www.wix.com/corvid/reference/wix-fetch.html
https://support.wix.com/en/article/corvid-accessing-third-party-services-with-the-fetch-api
https://support.wix.com/en/article/corvid-web-modules-calling-server-side-code-from-the-front-end

You can find out more with these links too.
https://www.andreasreiterer.at/single-result-with-promise-all/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
https://alligator.io/js/async-functions/
https://medium.com/@SteelKiwiDev/asynchronous-javascript-programming-with-promises-8c9a3661ccc4
https://medium.com/front-end-weekly/callbacks-promises-and-async-await-ad4756e01d90

Your’e missing the "return"s

export function promiseall() {
let fcaarray = [];
return getarray().then((result) => {
for (var i = 0; i < result.items.length; i++) {
fcaarray[i] = getfcainfo(result.items[i].frn);
}
return Promise.all(fcaarray);
}).then(info => {
console.log("Here");
return info;
});
}

Thx, works a treat now had to change 1 line to get it to work though

fcaarray[i] = getfcainfo(result.items[i].frn);

to

fcaarray.push(getfcainfo(result.items[i].frn));

Not sure why but to be honest dont care as it works now :). Thank you so much

@tom66859 You’re welcome. I don’t know why the difference. :slight_smile: