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
2 Answers
Reset to default 4Because 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);