question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[1.0.0-beta13] Async options with Promises displays [object Promise] as input value

See original GitHub issue

When loading options async with Promises, like in the example [object Promise] string is set as input value of Select

const getOptions = (input) => {
  return fetch(`/users/${input}.json`)
    .then((response) => {
      return response.json();
    }).then((json) => {
      return { options: json };
    });
}

<Select.Async
    loadOptions={getOptions}
/>

It is because of #907.

loadOptions from Async component attaches to onInputChange on Select component. so this

if (this.state.inputValue !== event.target.value && this.props.onInputChange) {
    let nextState = this.props.onInputChange(newInputValue);
    if (nextState != null) {
        newInputValue = '' + nextState;
    }
 }

sets newInputValue to return value of loadOptions, which is [object Promise]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
mattparrillacommented, Jun 7, 2016

@JedWatson any time frame on when this might get released? I’m not familiar with the release cadence but would love to use fetch + react-select for async options!

1reaction
dvreed77commented, May 18, 2016

@VladimirPal I’m closer… I was using this example code, which doesn’t seem to work:

const getOptions = (input) => {
  return fetch(`/users/${input}.json`)
    .then((response) => {
      return response.json();
    }).then((json) => {
      return { options: json };
    });
}

<Select.Async
    name="form-field-name"
    value="one"
    loadOptions={getOptions}
/>

and now am using something like this:

getUsers: function (input, callback) {
            console.log(input);
            fetch(`/users/${input}.json`)
                .then(function(response) {
                    return response.json()
                })
                .then(function(json) {
                    var data = {
                        options: json,
                        complete: false
                    };
                    callback(null, data);
                });

        },

This still isn’t quite working, but am not getting the [object Promise] in select window.

The optionRenderer is only firing on first character, even though I can see elements coming back

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async function returns [object Promise] instead of the real value
Async functions would always return a Promise . Promises may be resolved or rejected. You can handle promises in the following ways:.
Read more >
Promise - JavaScript - MDN Web Docs
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Read more >
Why am I getting [object Promise] when calling async function ...
Why? You are getting [object Promise] as value because you are calling an asynchronous function inside a synchronous code. This means the ...
Read more >
$q - AngularJS: API
function asyncGreet(name) { // perform some asynchronous operation, resolve or reject the promise when appropriate. return $q(function(resolve, reject) ...
Read more >
NetSuite Applications Suite - Promise Object
With promises, developers can write asynchronous code that is intuitive and efficient. A promise holds one of the following values: fulfilled – The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found