Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async/Await example help - can't access data in response #268

Open
crackernutter opened this issue Mar 9, 2022 · 3 comments
Open

Async/Await example help - can't access data in response #268

crackernutter opened this issue Mar 9, 2022 · 3 comments
Labels

Comments

@crackernutter
Copy link

crackernutter commented Mar 9, 2022

Can some explain how to create an async/await function in ijavascript so I can get access to the actual data?

const makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}
data = makeRequest(config)

printing data in a cell returns

Promise {
  {
    objectIdFieldName: 'OBJECTID',
    uniqueIdField: { name: 'OBJECTID', isSystemMaintained: true },
    globalIdFieldName: '',
    geometryType: 'esriGeometryPoint',
    spatialReference: { wkid: 4269, latestWkid: 4269 },
    fields: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],

So I can tell the request resolved, but then typing

data['objectIdFieldName']

returns nothing.
How do I access the actual data? I've tried JSON.parse, but that doesn't work either.

@crackernutter crackernutter changed the title Async/Await example help - can't access data in request Async/Await example help - can't access data in response Mar 9, 2022
@n-riesco
Copy link
Owner

n-riesco commented Mar 9, 2022

IJavascript behaves like the Node.js REPL does. That means await is not supported in the global context. Depending on your needs, here are a few solutions:

If you need to set data to the resolved value, something like this should work (untested):

var data;
axios.request(config).then((req) => {data = req.data;});

If you want to print the resolved value of a Promise, rather than the Promise, IJavascript offers a helper function for that:

const makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}
$$.sendResult(makeRequest(config));

IJavascript also offers a setting to do this automatically for every Promise, e.g.:

$$.config.awaitExecution = true;

const makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}
makeRequest(config);

@crackernutter
Copy link
Author

Thank you! Your first example worked for me, which suffices for my purposes.

However, I tried your last example too (I prefer async await syntax)

$$.config.awaitExecution = true;

const makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}
const result = makeRequest(config);
Object.keys(result)

It still wouldn't work - Object.keys(result) returns an empty array, but printing result to the console clearly shows the data in the response.

@n-riesco
Copy link
Owner

n-riesco commented Mar 9, 2022

That isn't going to work. Object.keys only returns enumerable keys (as you've discovered a Promise instance doesn't have any).

And $$.config.awaitExecution = true; only works, when the result of executing a cell is a Promise.

I guess what you're after is this:

var makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}

var result = makeRequest(config);
result.then(Object.keys).then(console.log);

Or, since you're playing with asynchronous code, see the documentation for $$.async():

var makeRequest = async (config) => {
    const req = await axios.request(config);
    return req.data
}

var result = makeRequest(config);

$$.async();
result.then(Object.keys).then($$.sendResult);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants