How do I rewrite a series of conditional statements with Q promises in node.js?

Shamoon
exports.create = function(req, res) {
  var company_id = req.company_id;
  var client = new Client(req.body);

  Company.findOne({_id: company_id}, function(err, company) {
    if(err) {
      response = {
        status: 'error',
        error: err
      }

      return res.json(response);
    } else if(!company) {
      response = {
        status: 'error',
        error: 'Invalid company_id'
      }

      return res.json(response);
    } else {
      client.save(function(err) {
        if(err) {
          response = {
            status: 'error',
            error: err
          }
        } else {
          response = {
            status: 'ok',
            client: client
          }
        }

        return res.json(response);
      });
    }
  });
}

That's my code (using Express, if it matters). I'm trying to learn more about promises, specifically with Q. I feel that this is the perfect kind of logic that can be implemented with promises to avoid this hairy conditional nest. But I'm not sure how to start?

Bergi

But I'm not sure how to start?

Start by removing the callbacks and using the Promise methods instead. Then put the error handling in a dedicated error callback, instead of using that condition. Also, you can put the code for building that response object in the very end (removing the duplication), and only pass/throw the err down to it.

exports.create = function(req, res) {
  var client = new Client(req.body);

  Q.ninvoke(Company, "findOne", {_id: req.company_id}).then(function(company) {
    if(!company)
      throw 'Invalid company_id';
    else
      return company;
  }).then(function(company) {
    return Q.ninvoke(client, "save");
  }).then(function(saveResult) {
    return {
      status: 'ok',
      client: client
    };
  }, function(err) {
    return {
      status: 'error',
      error: err
    };
  }).done(function(response) {
    res.json(response);
  });
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Q.js: How can I rewrite an async series flow in Q.js?

From Dev

How can I DRY this series of conditional statements?

From Dev

How to correctly chain conditional(?) promises with Q.js

From Dev

Node.js and Q Promises: How can I hand over parameters in a cleaner way?

From Dev

How do I simplify a conditional with multiple or statements?

From Dev

How do I sequentially chain promises with angularjs $q?

From Dev

Node.js Q Promises Multiple Parameters

From Dev

Node.js Q promises then() chaining not waiting

From Dev

How to do url rewrite in node.js?

From Dev

How do I handle exceptions globally with native promises in node.js?

From Dev

How do I use promises in node.js to clean up a chain of callbacks?

From Dev

Rewrite a series of if statements in Clojure

From Dev

how do i do conditional branching on a elements of a pandas series?

From Dev

How can I make a waterfall Q promises?

From Dev

How I should use promises for db + http calls in node js

From Dev

How do I chain nested promises with Q? I can't get them to run in proper order

From Dev

How do I rewrite this conditional as one conditional instead of two nested ones?

From Dev

Using Q Promises to chain GET requests in node.js

From Dev

Trouble to synchronise promises in Node.js using Q

From Dev

How to rewrite node js request

From Dev

How to use Q library while converting Node async functions to promises?

From Dev

how to use q promises?

From Dev

Concurrency limit in Q promises - node

From Dev

Simple Q Promises Node Example

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

How can I write a conditional without any conditional statements or operators?

From Java

How to use MongoDB with promises in Node.js?

From Dev

chaining promises with q.js

From Dev

How do I apply conditional formatting using datatables.js?

Related Related

  1. 1

    Q.js: How can I rewrite an async series flow in Q.js?

  2. 2

    How can I DRY this series of conditional statements?

  3. 3

    How to correctly chain conditional(?) promises with Q.js

  4. 4

    Node.js and Q Promises: How can I hand over parameters in a cleaner way?

  5. 5

    How do I simplify a conditional with multiple or statements?

  6. 6

    How do I sequentially chain promises with angularjs $q?

  7. 7

    Node.js Q Promises Multiple Parameters

  8. 8

    Node.js Q promises then() chaining not waiting

  9. 9

    How to do url rewrite in node.js?

  10. 10

    How do I handle exceptions globally with native promises in node.js?

  11. 11

    How do I use promises in node.js to clean up a chain of callbacks?

  12. 12

    Rewrite a series of if statements in Clojure

  13. 13

    how do i do conditional branching on a elements of a pandas series?

  14. 14

    How can I make a waterfall Q promises?

  15. 15

    How I should use promises for db + http calls in node js

  16. 16

    How do I chain nested promises with Q? I can't get them to run in proper order

  17. 17

    How do I rewrite this conditional as one conditional instead of two nested ones?

  18. 18

    Using Q Promises to chain GET requests in node.js

  19. 19

    Trouble to synchronise promises in Node.js using Q

  20. 20

    How to rewrite node js request

  21. 21

    How to use Q library while converting Node async functions to promises?

  22. 22

    how to use q promises?

  23. 23

    Concurrency limit in Q promises - node

  24. 24

    Simple Q Promises Node Example

  25. 25

    How can I write a conditional without any conditional statements or operators?

  26. 26

    How can I write a conditional without any conditional statements or operators?

  27. 27

    How to use MongoDB with promises in Node.js?

  28. 28

    chaining promises with q.js

  29. 29

    How do I apply conditional formatting using datatables.js?

HotTag

Archive