Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
How can I return my data in this nested promise model?

I have this function, I created it but then I'm getting confused and don't know how to return the data.

I have tried Promise.all() before but it's seems I do not quite understand it so I have removed it from my code, I don't know if it's a correct way to do it or not.

I'm following this AniList Node Document

Here's how the code work. I'm using POSTMAN to query the Title, for example, One Piece, it'll then search the query title and get the ID of that Title in AniList. Then, it's using that ID to find all the info (it's in the detailInfo)

Here's my Model:

static async getAnilist(title) {
const Anilist = new anilist()

const animeInfo = Anilist.searchEntry
    .anime(title, null, 1, 1)
    .then((titleToID) => {
        const animeID = titleToID.media[0].id
        const detailInfo = Anilist.media.anime(animeID).then((data) => {
            return {
                AnimeID: animeID,
                Schedule: data.airingSchedule[0],
                Score: data.averageScore,
                BannerImg: data.bannerImage,
                Character: data.characters,
                Country: data.countryOfOrigin,
                CoverImg: data.coverImage,
                Duration: data.duration,
                EndDate: data.endDate,
                EpisodeTotal: data.episodes,
                Genre: data.genres,
                Season: data.season,
                SeasonYear: data.seasonYear,
                Status: data.status,
                Studio: data.studios,
                UpdateAt: data.updatedAt,
            }
        })

        return detailInfo
    })
return animeInfo
}

Here's my Controller:

static async getAnilist(req, res, next) {
    const { title } = req.query
    try {
        const { data } = await Model.getAnilist(title)

        res.json({
            success: true,
            data: data,
        })
    } catch (err) {
        next(err)
    }
}

What I'm hoping for:

"success" : true,
"data" : {
     AnimeID,
     Schedule,
     Score,
     BannerImg,
     ...
     UpdateAt
}

What I'm getting right now

"success" : true

but without any data due to I can't return it.

The request is succeeded, but I don't know how to actually return it from nested promise.

Here's what I get from using console.log({AnimeID, Schedule...}) instead of return

INFO

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In async...await, async expects an await to follow. In your model you are declaring the function as async but inside you have promise. Easiest solution is to use await instead of promise.

static async getAnilist(title) {
const Anilist = new anilist()

const titleToId = await Anilist.searchEntry.anime(title, null, 1, 1);
const animeID = titleToID.media[0].id;
const data = await Anilist.media.anime(animeID);
const detailInfo = {
                AnimeID: animeID,
                Schedule: data.airingSchedule[0],
                Score: data.averageScore,
                BannerImg: data.bannerImage,
                Character: data.characters,
                Country: data.countryOfOrigin,
                CoverImg: data.coverImage,
                Duration: data.duration,
                EndData: data.endDate,
                EpisodeTotal: data.episodes,
                Genre: data.genres,
                Season: data.season,
                SeasonYear: data.seasonYear,
                Status: data.status,
                Studio: data.studios,
                UpdateAt: data.updatedAt,
            };

const animeInfo = detailInfo;

return animeInfo;
}

NB: You can optimize the above to be more consise. I translated it as-is.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda