简体   繁体   中英

Js promise for calculator

I have a simple calculator function that I wrote for a coding challenge. Now I'm having trouble with an additional calculate function that should return a promise.

class Calculator{
    constructor(){
        this[Symbol.toStringTag] = 'Calculator'; 
    } 
    add(a, b) {
        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:

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. I fear I'm going about it all wrong. How can I make it work?

Some remarks on your attempt:

  • The requirements do not suggest that you need to delay a result using setTimeout .

  • There is no need for ...args as parameter list for calculate . It will only get one argument, and it should be a 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 ).

  • resolve should be called with the result as argument.

  • reject should be called with NaN as argument

  • There is no specification that when the result is NaN , the promise should be rejected. The opposite is true: when the promise rejects (because of an error) it should reject with NaN as reason. 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. And then it should return a rejected promise.

We can avoid calling the Promise constructor by declaring the method as async . Here is an implementation that passes those tests:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM