# 计算器用js promise

[英]Js promise for calculator

I have a simple calculator function that I wrote for a coding challenge.我有一个简单的计算器 function，是我为编码挑战而编写的。 Now I'm having trouble with an additional `calculate` function that should return a promise.现在我遇到了额外`calculate` function 的问题，它应该返回 promise。

``````class Calculator{
constructor(){
this[Symbol.toStringTag] = 'Calculator';
}
return a + b;
}
subtract(a, b) {
return a - b;
}
multiply(a, b) {
return a * b;
}
divide(a, b) {
if(b === 0){
return NaN;
}
return a / b;
}
toString(){
return "Calculator";
}
calculate(...args) {
var result = 0;
return new Promise(function(resolve, reject){
setTimeout(function() {
if(result === NaN) {
reject(NaN);
} else {
resolve(result);
}
}, 1000);
});
}
}
``````

And here are the tests that the promise needs to meet:以下是 promise 需要满足的测试：

``````describe( "Calculator.calculate", function(){
let calculator;

beforeEach( function(){
calculator = new Calculator();
} );

it( "returns a promise", function( done ){
const
callDone = () => done(),
calculating = calculator.calculate( () => void 0 );
expect( calculating ).to.be.instanceOf( Promise );
calculating.then( callDone ).catch( callDone );
} );

it( "resolves with the result when the calculation succeeds", function( done ){
const calculating = calculator.calculate( function(){
expect( this ).to.equal( calculator );
let result = 0;
result += this.add( 1, 2 );
result += this.add( 3, 4 );
return result;
} );
calculating
.then( function( result ){
expect( result ).to.equal( 10 );
done();
} )
.catch( () => done() );
} );

it( "rejects with NaN when the calculation fails", function( done ){
const calculating = calculator.calculate();
calculating.catch( function( result ){
expect( result ).to.be.NaN;
done();
} );
} );
} );
``````

The above `calculate` function I wrote only passes the first test and none of the other.以上`calculate` function 我写的只通过了第一个测试，其他都没有。 I fear I'm going about it all wrong.我担心我做错了。 How can I make it work?我怎样才能让它发挥作用？

• The requirements do not suggest that you need to delay a result using `setTimeout` .这些要求并不建议您需要使用`setTimeout`延迟结果。

• There is no need for `...args` as parameter list for `calculate` .不需要`...args`作为`calculate`的参数列表。 It will only get one argument, and it should be a function.它只会得到一个参数，它应该是一个 function。

• The `result` variable is never set to anything else than 0. It would need to be the result by calling the callback function (the argument passed to `calculate` ). `result`变量永远不会设置为 0 以外的任何值。它需要是调用回调 function（传递给`calculate`参数）的结果。

• `resolve` should be called with the result as argument. `resolve`应该以结果作为参数调用。

• `reject` should be called with `NaN` as argument `reject`应该以`NaN`作为参数调用

• There is no specification that when the result is `NaN` , the promise should be rejected.没有规定当结果为`NaN`时，应拒绝 promise。 The opposite is true: when the promise rejects (because of an error) it should reject with NaN as reason.反之亦然：当 promise 拒绝（因为错误）时，它应该以 NaN 为理由拒绝。 The last test in the test sequence, passes no argument to `calculate` , which would lead to an error while `calculate` tries to execute an argument that is undefined.测试序列中的最后一个测试没有将参数传递给`calculate` ，这会在`calculate`尝试执行未定义的参数时导致错误。 And then it should return a rejected promise.然后它应该返回一个被拒绝的 promise。

We can avoid calling the Promise constructor by declaring the method as `async` .我们可以通过将方法声明为`async`来避免调用 Promise 构造函数。 Here is an implementation that passes those tests:这是通过这些测试的实现：

``````    async calculate(f) {
try {
return f.call(this);
} catch {
throw NaN;
}
}
``````