最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check this promise state, when programming a new promise? js - Stack Overflow

programmeradmin2浏览0评论

I am programming a new promise, it has many different conditions that call reject() or resolve() related to their state, also I know that the promise state will set with the first call to reject() | resolve(). My question is: Is there any native (build-in) way to get the promise state? The following is a demonstrative-code:

exports.addStatement = function (db, description, data) {
    return new Promise(function (resolve, reject) {
        validator.validateStatement(description, data)
        .then(function (data) {
            //......
            if(cnd1)
               resolve(res);
            if(cnd2)
               reject(err);
            //......
            //How to check if this promise is rejected or resolved yet? 

        })
        .catch(function (err) {
            reject(err);
        })
    })
};

I am programming a new promise, it has many different conditions that call reject() or resolve() related to their state, also I know that the promise state will set with the first call to reject() | resolve(). My question is: Is there any native (build-in) way to get the promise state? The following is a demonstrative-code:

exports.addStatement = function (db, description, data) {
    return new Promise(function (resolve, reject) {
        validator.validateStatement(description, data)
        .then(function (data) {
            //......
            if(cnd1)
               resolve(res);
            if(cnd2)
               reject(err);
            //......
            //How to check if this promise is rejected or resolved yet? 

        })
        .catch(function (err) {
            reject(err);
        })
    })
};
Share Improve this question edited Aug 8, 2016 at 7:16 Reza Afzalan asked Aug 8, 2016 at 7:03 Reza AfzalanReza Afzalan 5,7563 gold badges28 silver badges45 bronze badges 6
  • That's not how promises work. Seems like you've missed the whole concept of promises. You can only use then() and catch(), only after the promise has been returned from the function – Roysh Commented Aug 8, 2016 at 7:12
  • new Promise state is related to an inner promise, it works. – Reza Afzalan Commented Aug 8, 2016 at 7:20
  • 1 Two remarks. Why don't you just use if () {} else if() {} else {} or just return resolve();? That way there is no need to know the promise state at all and your code gets cleaner. And second: Why do you even need new Promise when validator.validateStatement already returns a promise? – str Commented Aug 8, 2016 at 7:24
  • If you want to know whether you just called resolve or reject yet inside your own function, then just keep your own flag that indicates that. But, probably you could just use if/else if /else to know whether you already hit one of your conditions or not. – jfriend00 Commented Aug 8, 2016 at 7:25
  • Also, your using an anti-pattern when creating a new outer promise here. You can just return the promise that validator.validateStatement() already returns. – jfriend00 Commented Aug 8, 2016 at 7:27
 |  Show 1 more ment

1 Answer 1

Reset to default 4

You cannot directly examine the state of a promise. That's not how they work. You can use .then() or .catch() on them with a callback to get notified.

Or, in your specific case, you can probably change the way your code is structured to remove the anti-pattern of creating an unnecessary outer promise and switching your logic to if/else if/else.

Here's the cleaned up code:

exports.addStatement = function (db, description, data) {
    return validator.validateStatement(description, data)
        .then(function (data) {
            //......
            if(cnd1) {
               // make res be the resolved value of the promise
               return res;
            } else if(cnd2) {
               // make promise bee rejected with err as the reason
               throw err;
            } else {
               // decide what else to do here
            }
        })
    })
};

If you couldn't make an if/else work for you, the above structure should still work because both the return and the throw terminate the execution of the .then() handler. So, the only code that continues after them is code that has not yet set the resolved/rejected value for the current promise so you don't have to look at the state of the promise to know that. If the code gets past the return and throw and is still executing, then neither of those was executed and the resolved/rejected value of the current promise is still unset.

发布评论

评论列表(0)

  1. 暂无评论