Как я могу сделать sh объект обещанием в vueJS? - PullRequest
0 голосов
/ 29 мая 2020

У меня есть следующая функция для загрузки категорий в vue JS

const self = this as any;
self.fetchFeaturedCategories();

, если я консоль веду журнал self.fetchFeaturedCategories (), он вернет обещания. enter image description here

Я хотел бы добиться чего-то вроде этого (это в angular):

this.categories = categories.map(category => {
const parentCategory = {
  gender: category.gender,
  id: category.id,
  imageUrl: category.imageUrl,
  isFeatured: category.isFeatured,
  name: 'All ' + category.name,
  parent: category.parent,
  parentId: category.parentId,
  slug: category.slug,
  type: category.type,
};
  category.children = [parentCategory, ...category.children];
  return category;
});

Однако я хотел бы вставить объект каждому детскому массиву. Как я могу этого добиться? Спасибо!

1 Ответ

0 голосов
/ 29 мая 2020

Чтобы управлять разрешенными значениями Promise, вы должны использовать .then или await, чтобы дождаться разрешения Promise, а затем действовать в соответствии с разрешенным значением и возвращать новое разрешенное значение.

  self.fetchFeaturedCategories() // assuming this is an array of Promises
   .then(categories => categories.map(category => {
      const parentCategory = {
        gender: category.gender,
        id: category.id,
        imageUrl: category.imageUrl,
        isFeatured: category.isFeatured,
        name: 'All ' + category.name,
        parent: category.parent,
        parentId: category.parentId,
        slug: category.slug,
        type: category.type,
      };
      const categoryMerged = {
         ...category, children: [parentCategory, ...category.children||[]]
      };
      return categoryMerged;
   }));
...