简体   繁体   English

计算器功能的JavaScript承诺

[英]javascript promise for calculator function

Been trying to figure out this coding challenge and I've hit a wall. 一直在试图解决这个编码挑战,我碰壁了。 It's either way over my head or I'm just missing something obvious. 要么让我头疼,要么我只是想念一些明显的东西。 Below is the code I've got so far as well as the mocha test I'm trying to create the function to satisfy. 下面是到目前为止我要尝试创建的函数所需要的代码以及mocha测试。

// Setup Mocha and Chai //设置摩卡和柴

mocha.setup( "bdd" );
var expect = chai.expect;

class Calculator {

  add(x, y) {
    return x + y;
  }

  subtract(x, y) {
    return x - y;
  }

  multiply(x, y) {
    return x * y;
  }

  divide(x, y) {
    if(y === 0) {
      return NaN;
    } else {
      return x / y
    }
  }

  calculate(...args) {
    var result = 0;
    return new Promise(function(resolve, reject){
      setTimeout(function() {
       if(result === NaN) {
         reject();
       } else {
         resolve();
       }
      }, 1000);
    });
  }
}

/** * 4. Add a calculate function to Calculator that matches this specification */ / ** * 4.向符合此规范的计算器添加一个计算功能* /

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

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

  it( "returns a promise", function(){
    var calculating = calculator.calculate( function(){} );
    expect( calculating ).to.be.instanceOf( Promise );
  } );

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

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

// Run the tests //运行测试

mocha.run();

The Calculator class was for a different test. 计算器类用于其他测试。 I'm having trouble with the calculate function and getting it to pass the test here at the bottom. 我在计算功能上遇到麻烦,无法通过底部的测试。 Any thought or insights? 有什么想法或见解吗?

** This is the error I get -- Error: Timeout of 2000ms exceeded. **这是我得到的错误-错误:超时超过2000毫秒。 For async tests and hooks, ensure "done()" is called; 对于异步测试和挂钩,请确保调用了“ done()”; if returning a Promise, ensure it resolves. 如果返回承诺,请确保其解决。 https://cdnjs.cloudflare.com/ajax/libs/mocha/4.0.1/mocha.min.js:1:38622 https://cdnjs.cloudflare.com/ajax/libs/mocha/4.0.1/mocha.min.js:1:38622

Thanks! 谢谢!

Your code is a bit messy, but I give you an example, using your code, on how to do a promise for a calculator function. 您的代码有点混乱,但是我使用您的代码为您提供了一个示例,说明如何对计算器功能作出承诺。

The promise function must always wrap the asynchronous function around. promise函数必须始终包装异步函数。 Then you call resolve or reject accordingly, whether there is an error. 然后,无论是否有错误,您都调用相应的resolvereject

 /* First define the calculator with the promise*/ function calculateDivision(x,y, time) { var result = 0; return new Promise(function(resolve, reject){ setTimeout(function() { result = x/y; if(!isFinite(result)) { reject(result); } else { resolve(result); } }, time); }); } /*now define the calculator using promise*/ function divide(x,y, time){ calculateDivision(x,y, time).then(function(result){ console.log("success:" + result); }, function(reason){ console.log("error: " + reason); }); } /*results will come inverted cause of time to calculate*/ divide(9,3, 2000); //divide 9 by 3 after 2 seconds divide(1,0, 1000); //divide 1 by 0 after 1 second divide(1000, 2, 500); //divide 1000 by 2 after 500 miliseconds 

Run this script and you'll see 运行此脚本,您将看到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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