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


Solution 1:

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.