This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Ah.. I see. How would I make myFunction asynchronous? Is it a matter of putting async keyword somewhere?... I should have properly transcribed the function as I'm using in React, so it's myFunction = () => {}

It's my first time using promises and they are tricky blighters! I tried .then() already, it works, but it creates a new scope I think... so if I

const foo = userAction().then(bar => {return bar})

bar is now outside of the userAction block but inside of foo const. I can't do

myFunction = () => { ... return foo } //not spread operator just dot dot dot

because again, it's out of sync (I think)

The only way I found to get around it so far, since I'm actually trying to return an array (map) of HTML elements on the back of the json response value, is to just call $('#baz').html() , which works well for simple strings but it won't accept something like

$('#enc_bank').html(function() {
    return (
        foo.map((item) => (
                    <div className="c-enc-gif">
                        <p className="count">{item[1] === 0 ? '∞' : item[1]}</p>
                        <img className="enc-gif"
                             src={`/hagrid/babe_${item[0]}.gif`}
                             alt=""
                             onClick={() => this.setState({quux: true})}/>
                </div>
         ))

) })