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

javascript - Getting an error when trying to write a promise: Promise resolver object is not a function - Stack Overflow

programmeradmin0浏览0评论

Could someone tell me what I am doing wrong when I'm writing this promise? I want the first function in calculate to execute and then setState of the ponent after the calculations are plete.

var calculate = new Promise({

            function(resolve, reject) {
                var newSubtotal = 0;
                var newTotal = 0;

                this.props.cart.map((product)=>{

                    newSubtotal += product.price;
                    newTotal += product.price;
                    newTotal *= 1.10;

                });
                resolve(newSubtotal, newTotal);
            }
        });

        calculate.then(() => {
            this.setState({
              subtotal: newSubtotal,
              total: newTotal
            });
        });

Could someone tell me what I am doing wrong when I'm writing this promise? I want the first function in calculate to execute and then setState of the ponent after the calculations are plete.

var calculate = new Promise({

            function(resolve, reject) {
                var newSubtotal = 0;
                var newTotal = 0;

                this.props.cart.map((product)=>{

                    newSubtotal += product.price;
                    newTotal += product.price;
                    newTotal *= 1.10;

                });
                resolve(newSubtotal, newTotal);
            }
        });

        calculate.then(() => {
            this.setState({
              subtotal: newSubtotal,
              total: newTotal
            });
        });
Share Improve this question asked Apr 22, 2016 at 9:19 MjuiceMjuice 1,2982 gold badges14 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Because you are passing object but function is expected

it should be something like this

var calculate = new Promise(function(resolve, reject){
// some stuff
})

In your code

 calculate.then(() => {
        this.setState({
          subtotal: newSubtotal,
          total: newTotal
        });
    });

has no access to newSubtotal, newTotal.

How about?

 calculate.then((newSubtotal, newTotal) => {
        this.setState({
          subtotal: newSubtotal,
          total: newTotal
        });
    });

Also, add: reject(someFailCase);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论