JavaScript Async function returns [object Promise] instead of the real value async function returns promise,async function returns undefined,async function

I'm very new to ReactJS.

I have some problem with the return value of async function

when I call

const result = this.getFieldsAPI();

the result value is [object Promise]

I see [object Promise] from console.log("result : " + result);

getFieldsAPI = async() => {
    let currentChromosome = "";
    switch (this.state.chromosome) {
      case "Autosom":
        currentChromosome = "/getlocusautosomalkit/";
        break;
      case "Y_STRs":
        currentChromosome = "/getlocusykit/";
        break;
      case "X_STRs":
        currentChromosome = "/getlocusxkit/";
        break;
      default:
        currentChromosome = "";
    }
    let result = [];
    await Axios.get(API_URL + currentChromosome + this.state.currentKit).then((Response) => {
      Response.data.map((locus) => {
        result.push(locus);
      });
    })
    return "result";
  }

  // To generate mock Form.Item
  getFields() {
    const count = this.state.expand ? 10 : 6;
    const { getFieldDecorator } = this.props.form;
    const children = [];
    const result = this.getFieldsAPI();
    console.log("result : " + result);
    for (let i = 0; i < 10; i++) {
      children.push(
        <Col span={8} key={i} style={{ display: i < count ? 'block' : 'none' }}>
          <Form.Item label={`Field ${i}`}>
            {getFieldDecorator(`field-${i}`, {
              rules: [{
                required: true,
                message: 'Input something!',
              }],
            })(
              <Input placeholder="placeholder" />
            )}
          </Form.Item>
        </Col>
      );
    }
    return children;
  }
Answer:1

You're not waiting for the value of result so you just get an unfulfilled promise. If you change

const result = this.getFieldsAPI();

to

const result = await this.getFieldsAPI();

You'll get what you're after. You'll also need to make getFields() async.

Answer:2

To get a valid response you should adjust your code a bit, because currently you're returning the string "result" and not the array from your promise.

In your getFieldsApi method you can do following:

...
Response = await Axios.get(API_URL + currentChromosome + this.state.currentKit);
return Response.data.map((locus) => locus);

And you would call it like:

const result = await this.getFieldsApi();
Answer:3

There are two arrays: [ {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1}, {"id": "344430b94t4t34rwefewfdff", "quantity": 5}, {"id": "342343343t4t34rwefewfd53", "quantity": 3} ] and [ {"id":...

There are two arrays: [ {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1}, {"id": "344430b94t4t34rwefewfdff", "quantity": 5}, {"id": "342343343t4t34rwefewfd53", "quantity": 3} ] and [ {"id":...

  1. merge arrays values
  2. merge arrays values php
  3. javascript merge arrays values
  4. merge two arrays values
  5. javascript merge arrays unique values
  6. php merge arrays unique values
  7. jquery merge arrays unique values
  8. php merge arrays add values
  9. php merge two arrays values

I need to distinguish between Internet Explorer's builds 11.0.9600.XXXX and 11.0.10240.XXXX in my website. I tried simple JavaScript/Angular/JQuery plugins but they only give the major (11) and minor ...

I need to distinguish between Internet Explorer's builds 11.0.9600.XXXX and 11.0.10240.XXXX in my website. I tried simple JavaScript/Angular/JQuery plugins but they only give the major (11) and minor ...

I have an array like the following: data = [{'province': 'North', 'level': 1, 'count': 204}, {'province': 'South', 'level': 1, 'count': 24}, {'province': 'East', 'level': 1, 'count': 24}, ...

I have an array like the following: data = [{'province': 'North', 'level': 1, 'count': 204}, {'province': 'South', 'level': 1, 'count': 24}, {'province': 'East', 'level': 1, 'count': 24}, ...

  1. remove keys from dictionary python
  2. remove keys from keyboard
  3. remove keys from object javascript
  4. remove keys from macbook pro
  5. remove keys from array php
  6. remove keys from mechanical keyboard
  7. remove keys from laptop keyboard
  8. remove keys from ssh-agent
  9. remove keys from apple keyboard
  10. remove keys from keyboard to clean
  11. remove keys from macbook air
  12. remove keys from chromebook
  13. remove keys from hash ruby
  14. remove keys from dict python
  15. remove keys from json object
  16. remove keys from mac keyboard
  17. remove keys from dell keyboard
  18. remove keys from dict
  19. remove keys from redis
  20. remove keys from lenovo laptop

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...