Search code examples
node.jsexpressasync.jsasynccallback

using callback in a function in node.js


I had a code like this :

router.post('/call' , async (req , res)=>{
    try{
        await async.mapLimit(req.body.n , 5 , (id , callback)=> {
            ////do something
            callback("message")
        }, (err , rest)=>{
            if(err){
                res.json({message : err})
            }else{
                res.json({message : rest})
            }
        })

    }catch(err){
        res.json({message : err})
    }
})

and i want to make the "do something part" a function like this:

router.post('/call' , async (req , res)=>{
    try{
        await async.mapLimit(req.body.n , 5 , (id , callback)=> {
            addStudentID(req , res , id , callback)
        }, (err , rest)=>{
            if(err){
                res.json({message : err})
            }else{
                res.json({message : rest})
            }
        })

    }catch(err){
        res.json({message : err})
    }
})

my problem is that it seems that callback can not sent as a parameter to another function is there any solution to do that?


Solution

  • problem was problem with function usage and function declaration that were not sync not the callback part