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

Unable to use await #1902

Closed
koskimas opened this issue Jul 2, 2015 · 3 comments
Closed

Unable to use await #1902

koskimas opened this issue Jul 2, 2015 · 3 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@koskimas
Copy link

koskimas commented Jul 2, 2015

I have this simple test script:

async function test() {
  return 42; 
}

let x = await test();

And I run it using the command babel-node --stage 0 await-test.js and I get this:

SyntaxError: /Users/foo/bar/await-test.js: Unexpected token (5:14)
  3 | }
  4 |
> 5 | let x = await test();
    |               ^
babel-node --version
5.6.14

What am I doing wrong?

@sebmck
Copy link
Contributor

sebmck commented Jul 2, 2015

await can only be on the inside of async functions.

@sebmck sebmck closed this as completed Jul 2, 2015
@koskimas
Copy link
Author

koskimas commented Jul 2, 2015

Well that was fast :D. Thanks! So from an non-async function we must resolve the returned promise. Makes sense now that I think about it.

async function test() {
  return 42;
}

async function main() {
  return await test();
}

main().then(function (res) {
  console.log(res);
});

@sebmck
Copy link
Contributor

sebmck commented Jul 2, 2015

Yup, there's no way to block execution outside of them.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 14, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

2 participants