troubleshooting Question

Why does this work?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
6 Comments2 Solutions120 ViewsLast Modified:
Here's my code:

exports.deletePost = (req, res, next) => {
  const postId = req.params.postId;
    .then(post => {
      if (!post) {
        const error = new Error("Could not find post.");
        error.statusCode = 404;
        throw error;
      //check login user
      return Post.findByIdAndRemove(postId);
    .then(result => {
      res.status(200).json({ message: "Deleted post!" });
    .catch(err => {
      if (!err.statusCode) {
        err.statusCode = 500;

Line #12, it says return Post.findByIdAndRemove(postId).

I was always under the impression that once you invoke the "return" dynamic, you're exiting the flow of the code at that point. Yet, in this case, it proceeds to the next "then" clause.


The code works, but I want to know why the continues to the next "then" clause despite the fact that there's a "return" dynamic in place.
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros