sveltekit - Svelte onMount method does't wait for await - Stack Overflow

I have Firebase app with Svelte 5. Svelte is '5.17.5', @sveltejskit is '2.15.3', f

I have Firebase app with Svelte 5. Svelte is '5.17.5', @sveltejs/kit is '2.15.3', firebase is '11.2.0', vite is '6.0.7'. Here is method, it doesn't stop on await/async. What am I missing?

Here is definition:

import { getDownloadURL, getStorage, listAll, ref } from 'firebase/storage';
import { page } from '$app/state';
import { getZStorageI, zFirestore, zlataTrinket_kategorija, generateKeyword, type ZlataTrinket } from '$lib/common';

Here is code:

let pictureUrl: Array<string> = $state([]);

onMount(async () => {
  const _id = page.url.searchParams.get('id');
  const storage = getStorage();
  const listRef = ref(storage, getZStorageI(_id));
  let i = 0;

  await listAll(listRef).then(async (res) => {
    res.items.forEach(async (itemRef) => {
      await getDownloadURL(itemRef).then(async (url) => {
        console.log(url);
                    
        pictureUrl[i++] =  url;
      });
    });
  });

  console.log("It should be after?");
});

It show:

It should be after?
+page.svelte:44 .firebasestorage.app/o/zStorage%2F400_499%2F463%2F2.jpg?alt=media&token=fd48f7c8-d90d-4841-91e9-7fd3ca88780a
+page.svelte:44 .firebasestorage.app/o/zStorage%2F400_499%2F463%2F4.jpg?alt=media&token=2329c929-75b8-4251-a02e-82b31b2febcb
+page.svelte:44 .firebasestorage.app/o/zStorage%2F400_499%2F463%2F1.jpg?alt=media&token=5fbdb696-977b-498d-928a-3280843e4748
+page.svelte:44 .firebasestorage.app/o/zStorage%2F400_49

I have Firebase app with Svelte 5. Svelte is '5.17.5', @sveltejs/kit is '2.15.3', firebase is '11.2.0', vite is '6.0.7'. Here is method, it doesn't stop on await/async. What am I missing?

Here is definition:

import { getDownloadURL, getStorage, listAll, ref } from 'firebase/storage';
import { page } from '$app/state';
import { getZStorageI, zFirestore, zlataTrinket_kategorija, generateKeyword, type ZlataTrinket } from '$lib/common';

Here is code:

let pictureUrl: Array<string> = $state([]);

onMount(async () => {
  const _id = page.url.searchParams.get('id');
  const storage = getStorage();
  const listRef = ref(storage, getZStorageI(_id));
  let i = 0;

  await listAll(listRef).then(async (res) => {
    res.items.forEach(async (itemRef) => {
      await getDownloadURL(itemRef).then(async (url) => {
        console.log(url);
                    
        pictureUrl[i++] =  url;
      });
    });
  });

  console.log("It should be after?");
});

It show:

It should be after?
+page.svelte:44 https://firebasestorage.googleapis/v0/b/zlata-67787.firebasestorage.app/o/zStorage%2F400_499%2F463%2F2.jpg?alt=media&token=fd48f7c8-d90d-4841-91e9-7fd3ca88780a
+page.svelte:44 https://firebasestorage.googleapis/v0/b/zlata-67787.firebasestorage.app/o/zStorage%2F400_499%2F463%2F4.jpg?alt=media&token=2329c929-75b8-4251-a02e-82b31b2febcb
+page.svelte:44 https://firebasestorage.googleapis/v0/b/zlata-67787.firebasestorage.app/o/zStorage%2F400_499%2F463%2F1.jpg?alt=media&token=5fbdb696-977b-498d-928a-3280843e4748
+page.svelte:44 https://firebasestorage.googleapis/v0/b/zlata-67787.firebasestorage.app/o/zStorage%2F400_49
Share Improve this question edited Mar 10 at 12:59 brunnerh 186k30 gold badges357 silver badges430 bronze badges asked Mar 10 at 12:32 HrvojeHrvoje 1311 gold badge1 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

The res.items.forEach breaks the await chain.

If you need sequential processing use a regular for loop.
If not, use await Promise.all(res.items.map(async item => { ... })).

(Also note that having async code in onMount will not stop the component from rendering. It will also mess up any cleanup you try to do, i.e. a function returned from the onMount callback will not be executed this way.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744846838a4596889.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信