3

This works:

var getArray = function(id) {
  return [1, 2];
}

console.log(getArray()[0]); // returns 1

This does not work:

var getArray = async function(id) {
  return [1, 2];
}

console.log(await getArray()[0]); // syntax error

Why? How can I change the async/await syntax so it works?

1
  • You cannot use await outside of an async function. Wherever you call getArray, make that async as well Commented Dec 5, 2017 at 18:49

3 Answers 3

5

I found the solution, it's very simple. Just wrap the await part with parentheses.

var first = (await getArray())[0];
Sign up to request clarification or add additional context in comments.

5 Comments

Checked it out of curiosity but I can't see why it would work nor I can reproduce it - it still throws syntax errors at me. codesandbox.io/s/vpyqln4zl
"await" must always be wrapped with an async function. it works here perfectly: codesandbox.io/s/kw016lolvo
If you wrap it with async function, those parantheses are not necessary though...
no, they are necessary: this will throw undefined codesandbox.io/s/kw016lolvo
Ah, right. That's because you try to access 0 element before awaiting the result.
1

You can use await keyword in another async function to get the desired result via - (await getArray())[0].

If you use async function not inside another async function, it must be used like simple Promise function with calling then.

var getArray =  async function(id) {
    return [1,2];
}

getArray().then(res => console.log(res[0])).catch(err => {/* ... */});

If you want to use await, you need another wrapper async function which will do the logic there and will be just called. Also you can use try/catch within your function

var getArray =  async function(id) {
    return [1,2];
}

async function printFirst() {
  try {
     const first = (await getArray())[0];
     console.log(first);
  } catch(e) {
  
  }
}

printFirst();

6 Comments

I want to use async/await, not .then
See the updated answer. Also read the answer attentively. I have written where you can use await
@SurenSrapyan Your should add a note about uncaught errors if you don't have a .catch set.
@RyanZim Thanks. It was not necessary in this example, but for the whole picture I added.
i'm wondering if there is a more elegant solution with fewer lines, like with synchronous code.
|
0

A clearer way to do it is via ES6 destructuring:


const [ topBid ] = await getBids(symbol)

// ^^^ it's the same as:

const topBid = (await getBids(symbol))[0]
      

In case you need not the first element of an array, there're various destructuring patterns for different scenarios:

https://www.deadcoderising.com/2017-03-28-es6-destructuring-an-elegant-way-of-extracting-data-from-arrays-and-objects-in-javascript/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.