node.js ~ constructing chained sequence of Promise resolves

后端 未结 2 2069
醉梦人生
醉梦人生 2020-12-10 23:21

Can anyone suggest a better way to structure this use of Promises? I\'m newish to Promises and am wondering if I\'m missing something about how to construct this a chain of

2条回答
  •  一生所求
    2020-12-10 23:58

    Instead of something like this:

                    .then(function (rs) {
                        qry = '...';
                        req.query(qry)
                            .then(function (rss) {
    

    you can use something like this:

                    .then(function (rs) {
                        qry = '...';
                        return req.query(qry);
                    }).then(function (rss) {
    

    I.e. you can return a promise in one then callback and get the resolved value of that promise in the next then callback, so your indentation keeps constant.

    Simpler example - instead of this:

    a().then(va => {
        b(va).then(vb => {
            c(vb).then(vc => {
                // you can use vc here
            });
        });
    });
    

    you can do:

    a().then(va => {
        return b(va);
    }).then(vb => {
        return c(vb);
    }).then(vc => {
        // you can use vc here
    });
    

    Or, even simpler if you use async and await:

    va = await a();
    vb = await b(va);
    vc = await c(vb);
    // you can use vc here
    

    Note that you can only use await inside of a function created with the async keyword. In places where you don't have native support for async and await you can use Babel or with a slightly different syntax a generator based approach like in co or Bluebird coroutines. For more info and support in browsers and Node, see this answer:

    • Is there a better way to run CLI commands with Node.js?

    Update

    This is not tested, but this is more or less how I would write it:

    module.exports = {
    
        dbConnection: function () {
            return { user: 'sa', password: 'mypassword', server: 'localhost', database: 'mydb' };
        },
    
        CanIConnectToTheDB: function () {
            var sql = require('mssql');
            var myDao = require('./myDao');
            var cn = new sql.ConnectionPool(myDao.dbConnection());
            var req;
    
            return cn.connect()
            .catch(err => Promise.reject('Error 1: ' + err))
            .then(() => {
                req = new sql.Request(cn);
                var qry = 'select serverproperty(\'productversion\') as \'rs\'';
                return req.query(qry)
                    .catch(err => Promise.reject('Error 2: ' + err));
            }).then(rs => {
                var qry = 'select isnull(object_id(\'SomeObjectIKnowExists\'), -1)';
                return req.query(qry)
                    .catch(err => Promise.reject('Error 3: ' + err));
            }).then(function (rss) {
                return 'CONNECTED// MASTER DB SUCCESS// MY DB SUCCESS';
            }).catch(err => {
                // if you want it always resolved:
                return 'CAN NOT CONNECT: ' + err;
            });
        }
    };
    

    Of course I would keep the final promise returned by that function to be rejected on errors and resolved only on success, but since you explicitly included that strange requirement in your question then I wrote it how you wanted.

    But if it rejected the promise on any error then it would be much easier to use, especially if all you care is the answer to the question in the name of the function - Can I Connect To The DB:

    CanIConnectToTheDB()
        .then(() => console.log("Yes I can"))
        .catch(() => console.log("No I can't"));
    

    More info:

    For more info see those answers:

    • How to convert Waterfall method to promise
    • jQuery: Return data after ajax call success (see explanation in updates to this answer)

提交回复
热议问题