9

I have a problem with async/await. Here is my code:

import axios from 'axios'

export default async function getRequestedData (requestAddress, params) {
   return await axios.get(requestAddress, {params: params})
}

But instead of the result it returns a full promise, so the data is heavily nested inside a promise:

enter image description here

1
  • A client to get the requested data must invoke the async function so s/he has to await for the response async func() { [...] const data = await getRequestedData(requestAddress, params); // processing 'data' - js object .... } Commented Sep 21, 2018 at 11:47

5 Answers 5

9

Like Ha Ja said, I think you still need to resolve the promise. If you just return the await you're going to get a promise.

const fs = require ('fs')

function getText () {

    return new Promise( (resolve, reject) => {

        fs.readFile('./foo.txt', 'utf8', (err, data) => {
            if (err) {
                reject(err)
            }
                resolve(data)
            })
        })
}

async function output () {
    try {
        let result = await getText()
        console.log("inside try: ", result)
        return result
    }
    catch (err){
        console.log(err)
    }
}

console.log("outside: ", output())
output().then( result => console.log("after then: ", result))

// outside:  Promise { <pending> }
// inside try:  foo text
// inside try:  foo text
// after then:  foo text
Sign up to request clarification or add additional context in comments.

Comments

3

You have to return the data:

const response = await axios.get(requestAddress, {params: params})
return response.data;

6 Comments

Still a Promise but with diffrence [[PromiseValue]] has only content of data object
How about return await axios.get(requestAddress, {params: params}).then(response => response.data) ?
The same result as the last one
Could you share how you called the function getRequestedData?
This is a great response .. what does it look like, taking into account the reject block (I mean catch)?
|
2

One liner:

return (await axios.get(url)).data;

Comments

0

In React component you can do with this trick:

function InitProduct() {
    // Get your product from database
    var productId = 'abc';
    axios.get('/api/products/' + productId).then(response => {
        $('.' + productId).text(response.data.ProductTitle);
    });

    return (<div className={productId}></div>)
}

export class TestNow extends Component {
    render() {
        return (
            <div>
                {InitProduct()}
            </div>
        )
    }
}

Comments

0

An async function will always wrap what it returns in a promise so you should chain the caller with a then() method thereby resolving that promise for example: getRequestedData (requestAddress, params).then ( (data) => { ...do something... });

Or you could await the call to getRequestedData function, in which case the data inside the returned promise would be extracted for example:

let requestedData = await getRequestedData (requestAddress, params):

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.