Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
Call a function is JavaScript that calls a promise function and returns the result to the original function

I have two functions (One function within the controller and oen function in a service). I want to call the service function from the controller function. After calling teh service function this function calls a database function (using Sequelize).

Controller:

exports.getMilestoneById = async (req, res, next) => {
    const milestoneId = req.params.milestoneId;
    try{
    milestoneService.getMilestoneById(milestoneId)
    .then(mielstone => {
        console.log("Milestone in tehn: " + mielstone)
    });
    console.log("Milestone: " + milestone);
    res.status(200).json({message: 'Milestone fetched', milestone: milestone});
    } catch (error){
        if (!error.statusCode){
            error.statusCode = 500;
        }
    }
}

Service:

exports.getMilestoneById = async (milestoneId) => {

Milestone.findByPk(milestoneId)
.then(milestone => {
    console.log("In Service: " + milestone)
    return milestone.get();
})
.catch(err => {
    console.log("Error" + err);
});

}

The problem: I don't get a milestone back.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Ok, let's try to put some order here.

Promises Flow

Your code doesn't wait for the Promise response.

milestoneService.getMilestoneById(milestoneId) // <- this returns a promise
// all stuff below here doesn't wait for the promise to complete and can't see its result
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone: milestone});

Instead, writing something like:

milestoneService.getMilestoneById(milestoneId)
    .then(mielstone => {
        console.log("Milestone in tehn: " + mielstone);
        // I moved all the stuff that needs to wait in here!!
        console.log("Milestone: " + milestone);
        res.status(200).json({message: 'Milestone fetched', milestone: milestone});
    });

This would work as expected.

Async / Await

You added that async before the functions, I'd figure you could put that to good use! async / await helps you clean the promises flow by letting the wrapping function wait for their results, so writing something like:

    const milestone = await milestoneService.getMilestoneById(milestoneId)
    console.log("Milestone: " + milestone);
    res.status(200).json({message: 'Milestone fetched', milestone: milestone});

Would also work as expected.

Returns

... or not! You missed pretty much all returns in your code. As a rule of thumbs try to make every function always return something, especially if it's promises! So like:

exports.getMilestoneById = async (milestoneId) => {
 const milestone = await Milestone.findByPk(milestoneId);
 return milestone.get();
 /* I don't suggest handling the error here, because it doesn't bubble well! 
 /* If there's an error what would this function return?
}

Then

exports.getMilestoneById = async (req, res, next) => {
    const milestoneId = req.params.milestoneId;
    try {
       const milestone = await milestoneService.getMilestoneById(milestoneId)
       console.log("Milestone: " + milestone);
        // just return this
       return res.status(200).json({ message: 'Milestone fetched', milestone });
    } catch (error){
        if (!error.statusCode){
            error.statusCode = 500;
        }
        // call next with the error!
        return next(error);
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

When you are returning the result in this callback:

.then(milestone => {
    console.log("In Service: " + milestone)
    return milestone.get();
})

You are in fact setting the return value for the callback function, not the return value for your service function milestone service.getMilestoneById(). Because your function is an async function, you should use Javascript's async-await syntax, which will only get the value once the promise is resolved.

exports.getMilestoneById = async (milestoneId) => {
    const milestone = await Milestone.findByPk(milestoneId)
    .catch(err => {
        console.log("Error" + err);
    });

    console.log("In Service: " + milestone)
    return milestone
}

This should be the same for your controller function:

exports.getMilestoneById = async (req, res, next) => {
    const milestoneId = req.params.milestoneId;
    try{
    const milestone = await milestoneService.getMilestoneById(milestoneId)
    console.log("Milestone: " + milestone);
    res.status(200).json({message: 'Milestone fetched', milestone: 
    milestone});
    } catch (error){
        if (!error.statusCode){
            error.statusCode = 500;
        }
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!