Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   promise не передает результат (https://javascript.ru/forum/misc/79922-promise-ne-peredaet-rezultat.html)

Castromen 07.04.2020 23:50

promise не передает результат
 
Добрый вечер.
Пытаюсь реализовать последовательность, через Promise, и передать в функцию init(arr) массив данных, но приходит пустой.
let arr = []
      new Promise(function(resolve, reject) {
        setTimeout(() => { 
          resolve(spheres)
        }, 500)
      }).then(result => {
        this.setState({ loading: false });
        result.map(async it => {
          const uuid = it.imageUrl.split('/').pop();
          const url = await api.share.shareUrl(uuid);
          arr.push({
            title:it.title,
            imageUrl: url.data.data.url 
          })
        })
        return arr
      }).then(arr => {
        return init(arr)
      })

voraa 08.04.2020 06:51

Может так
let arr = []
      new Promise(function(resolve, reject) {
        setTimeout(() => { 
          resolve(spheres)
        }, 500)
      }).then(async result => {
        this.setState({ loading: false });
        await Promise.all (
          result.map(async it => {
            const uuid = it.imageUrl.split('/').pop();
            const url = await api.share.shareUrl(uuid);
            arr.push({
              title:it.title,
              imageUrl: url.data.data.url 
            })
          })
        )
        return arr
      }).then(arr => {
        return init(arr)
      })


Часовой пояс GMT +3, время: 18:50.