title: How to make CRUD in NodeJS but with async and await?
body: <p>I have to create CRUD but my controller uses a lot of callbacks. I do not know how to use <code>async</code> and <code>await</code> in order to make my code look clean and high-performance. I hope anyone can help me and give me some suggestions.</p>
  <p>Below is my controller:</p>
  <pre><code>// create new course
  export function createCourse (req, res) {
    const course = new Course({
      _id: mongoose.Types.ObjectId(),
      title: req.body.title,
      description: req.body.description,
    });
    
    return course
      .save()
      .then((newCourse) =&gt; {
        return res.status(201).json({
          success: true,
          message: 'New course created successfully',
          Course: newCourse,
        });
      })
      .catch((error) =&gt; {
          console.log(error);
        res.status(500).json({
          success: false,
          message: 'Server error. Please try again.',
          error: error.message,
        });
      });
  }
  
  // Get all course
  export function getAllCourse( req, res){
    Course.find()
      .select('_id title description')
      .then((allCourse) =&gt; {
        return res.status(200).json({
          success: true,
          message: 'A list of all course',
          Course: allCourse,
        });
      })
      .catch((err) =&gt; {
        res.status(500).json({
          success: false,
          message: 'Server error. Please try again.',
          error: err.message,
        });
      });
  }
  
  // get single course
  export function getSingleCourse(req, res) {
    const id = req.params.courseId;
    Course.findById(id)
      .then((singleCourse) =&gt; {
        res.status(200).json({
          success: true,
          message: `More on ${singleCourse.title}`,
          Course: singleCourse,
        });
      })
      .catch((err) =&gt; {
        res.status(500).json({
          success: false,
          message: 'This course does not exist',
          error: err.message,
        });
     });
  }
  
  // update course
  export function updateCourse(req, res) {
    const id = req.params.courseId;
    const updateObject = req.body;
    Course.update({ _id:id }, { $set:updateObject })
      .exec()
      .then(() =&gt; {
        res.status(200).json({
          success: true,
          message: 'Course is updated',
          updateCourse: updateObject,
        });
      })
      .catch((err) =&gt; {
        res.status(500).json({
          success: false,
          message: 'Server error. Please try again.'
        });
      });
  }
  
  // delete a course
  export function deleteCourse(req, res) {
    const id = req.params.courseId;
    Course.findByIdAndRemove(id)
      .exec()
      .then(()=&gt; res.status(204).json({
        success: true,
      }))
      .catch((err) =&gt; res.status(500).json({
        success: false,
      }));
  }
  </code></pre>
  <p>Now I want to replace all callbacks with async and await, how to replace and how to use them more correctly. I appreciate every help.</p>