JS:如何测试异步功能支持

时间:2018-01-22 05:24:27

标签: javascript node.js asynchronous async-await

我想知道是否有可能在JS中测试异步功能支持。

try {
    async () => {}
}
catch (err) {
    // No support
}

上述方法无效,因为它允许在Node v6中转义语法错误。

/secret/test.js:8
    async () => {}
          ^

SyntaxError: Unexpected token (

那么您认为测试异步支持的最佳(如果存在)方法是什么?

1 个答案:

答案 0 :(得分:2)

使用邪恶的评估

import { Form, Icon, Input, Button, Checkbox } from 'antd';
import {FormComponentProps} from 'antd/lib/form/Form';
const FormItem = Form.Item;
interface NormalLoginProps{}

class NormalLoginForm extends React.Component<NormalLoginProps & FormComponentProps> {
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSubmit} className="login-form">
        <FormItem>
          {getFieldDecorator('userName', {
            rules: [{ required: true, message: 'Please input your username!' }],
          })(
            <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('password', {
            rules: [{ required: true, message: 'Please input your Password!' }],
          })(
            <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="Password" />
          )}
        </FormItem>
        <FormItem>
          {getFieldDecorator('remember', {
            valuePropName: 'checked',
            initialValue: true,
          })(
            <Checkbox>Remember me</Checkbox>
          )}
          <a className="login-form-forgot" href="">Forgot password</a>
          <Button type="primary" htmlType="submit" className="login-form-button">
            Log in
          </Button>
          Or <a href="">register now!</a>
        </FormItem>
      </Form>
    );
  }
}

const WrappedNormalLoginForm = Form.create<NormalLoginProps>()(NormalLoginForm);

ReactDOM.render(<WrappedNormalLoginForm />, mountNode);

注意,如果引擎支持async / await但不支持箭头表示法,则会失败,但是,我认为不存在这样的引擎