Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Return promise in node.js

i try to return data in node.js from a APIs , but I'm having problems, because I need an asynchronous function, I couldn't understand for sure the correct use of the promise, I've tried everything and I couldn't put the result in the return, only in the console.log, somebody help me?

const express = require('express')
const MFA = require('mangadex-full-api')

module.exports = {
    async indexManga(req, res) {
         
        const mangalist = MFA.login('DarksGol', 'R@ul1605', './md_cache/').then(async () => {

            manga = []
                await MFA.Manga.search('Kiss').then(results => {
                results.forEach((elem, i) => {   
                    let obj = {}
                    obj.status = elem.status
                    obj.title = elem.title
                    
                    manga.push(obj)
                })
            }).catch(console.error)

            return manga

        }).catch(console.error) 

        console.log(await mangalist)
        return mangalist
    }
}

no error occurred, only infinite loading on request

const express = require('express')
const routes = express.Router()

const searchManga = require('../src/controllers/searchManga')

routes.get('/searchManga', searchManga.indexManga)

module.exports = routes
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I don't see why this would cause "infinite loading on request", but you could greatly simplify the code to just

const express = require('express');
const MFA = require('mangadex-full-api');

module.exports = {
    async indexManga(req, res) {
        await MFA.login('DarksGol', 'R@ul1605', './md_cache/')
        const mangaList = [];
        const results = await MFA.Manga.search('Kiss');
        results.forEach(elem => {   
            mangaList.push({
                status: elem.status,
                title: elem.title,
            });
        });
        return mangalist;
    },
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Looks like indexManga is an endpoint. Each endpoint function must end the request-response cycle by sending a response ( res.send(), res.json(), res.end(), etc). If indexManga s an endpoint, the solution would be:

...

//return mangalist
res.send(mangalist)

or

//return mangalist
res.json({ status: "success", message: "logged in successfully" })

If it is a meddleware:

async indexManga(req, res, next) {
  ...
  //return mangalist
  return next()
}

EDIT: you are using async/await with .then() improperly in some places. Try this way:

module.exports = {
    indexManga(req, res) {
        MFA.login('DarksGol', 'R@ul1605', './md_cache/').then(() => {

            manga = []
            MFA.Manga.search('Kiss').then(results => {
                results.forEach((elem, i) => {   
                    let obj = {}
                    obj.status = elem.status
                    obj.title = elem.title
                    
                    manga.push(obj)
                })
                
                res.json({ status: "success", data: manga })
                
            }).catch((err) => {
               res.json({ status: "fail", error: err })
            })
            

        }).catch((err) => {
           res.json({ status: "fail", error: err })
        })
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda