async function
returns Promise
, so by the time you execute IIFE, the Promise is still pending, as the result fetchGroupedWishlistProducts()
would not be resolved. So in your case, second console.log
will be executed first, and is undefined
To handle this, you could wrap it into another IIFE
let groupedWishlistProducts;
(async function() {
await (async function () {
groupedWishlistProducts = await fetchGroupedWishlistProducts();
console.log(groupedWishlistProducts); // logs returned value properly
})();
console.log(groupedWishlistProducts);
})()
or just resolve the promise
let groupedWishlistProducts;
(async function () {
groupedWishlistProducts = await fetchGroupedWishlistProducts();
console.log(groupedWishlistProducts); // logs returned value properly
})().then(() => {
console.log(groupedWishlistProducts);
})