Picks Prabhakar
Picks Prabhakar

Reputation: 335

Confused between promise and async/awaits

I'm a novice in Node JS. I practice promise and I successfully used it. What I understand is with using a promise you can hold the output and send resolve and reject. I used in database operation.

Then someone suggested me to use async/awaits. So here is my code which ran successfully the first time.

shop.js file

const models = require("../models");
const shopModel = models.Shop;
exports.checkShop = function(shopName) {
  return new Promise((reslove, reject) => {
    shopModel
      .findOne({ where: { shop: shopName } })
      .then(rs => {
        if (rs) {
          reslove(rs);
        }
      })
      .catch(err => {
        reject(err.toString());
      });
  });
};

And the file where i called this

const shopController = require("./shop");
exports.getInstall = function(req, res) {

  const shop = req.body.shop;

  if (!cn(shop)) {
    shopController
      .checkShop(shop)
      .then(
        shopCheck =>
          function() {
            if (shopCheck) {
                res.send(`Welcome back ${shopCheck.shop}`);
            } else {
           //my else stuff
            }
          }
      )
      .catch(
        e =>
          function() {
            res.state(500).send(e);
          }
      );
  } else {
    return res
      .status(400)
      .send(
        "Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request"
      );
  }
};

And this is how I tried to replace it with async/awaits. But it does not work.

exports.checkShop = async function(shopName) {
    try{
      var rs = await shopModel.findOne({ where: { shop: shopName } });
      if(rs){
        return rs;
      }
      else{
        return false;
      }
    }
    catch(e){
      return Promise.reject(new Error(400));
    }
};

And the other file

exports.getInstall = function(req, res) {
    const shop = req.body.shop;

    if (!cn(shop)) {
      var shopCheck =  shopController.checkShop(shop);
      try {
        if (shopCheck) {
          res.send(`Welcome back ${shopCheck.shop}`);
        } else {
//         else stuff
        }
      } catch (e) {
        res.state(500).send(e);
      }
    } else {
      return res
        .status(400)
        .send(
          "Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request"
        );
    }
  };

Upvotes: 2

Views: 97

Answers (1)

jonahe
jonahe

Reputation: 5000

Every function with the async keyword before it will (explicitly or implicitly) return a promise.

So when you call shopController.checkShop you will either have to do something like

shopController.checkShop().then(.... )

or make getInstall an async function as well so that you can use await inside it.

exports.getInstall = async function(req, res) {
   // other code here.. 
   const result = await shopController.checkShop(shop);
   //.. 
}

Edit:

If you want to make getInstall async and use await on checkShop you will have to catch the potential rejection using try {} catch like you did in checkShop.

Upvotes: 1

Related Questions