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

can I use it with es7 async/await? #27

Closed
vyorkin opened this issue Jul 5, 2015 · 8 comments
Closed

can I use it with es7 async/await? #27

vyorkin opened this issue Jul 5, 2015 · 8 comments

Comments

@vyorkin
Copy link

vyorkin commented Jul 5, 2015

example:

let fetch = require('node-fetch');

(async () => {
  try {
    // request
    let response = await fetch('http://localhost:8088/api/v1/hooy');
    // parsing
    let data = await response.json();
    console.log('data: ', data);
  } catch (error) {
    console.log('error: ', error);
  }
}());

sh session:

❯ es6 test1.js
error:  [TypeError: Cannot read property 'json' of undefined]

where es6 alias is:

function es6() { babel --stage=0 --experimental "$@" | iojs; }
@bitinn
Copy link
Collaborator

bitinn commented Jul 5, 2015

I haven't tried it, but I imagine babel await should happily accept a promise of any form, could you try using a native Promise to see if any setup is wrong?

@bitinn
Copy link
Collaborator

bitinn commented Jul 5, 2015

As in I don't think response should be undefined.

@bitinn
Copy link
Collaborator

bitinn commented Jul 10, 2015

Just asking, @vyorkin, did you manage to figure it out?

@vyorkin
Copy link
Author

vyorkin commented Jul 11, 2015

I didn't try, sorry, I realized that I only need fetch in browser.
btw this works just fine:

fetch('http://localhost:8088/api/v1/pizda')
  .then((response) => response.json())
  .then((data) => console.log('data: ', data))
  .catch((error) => console.log('error: ', error));

@tracker1
Copy link

If you're using babeljs or something else that will transpile async/await (and likely coroutines/promises) it will work... I've used it quite a bit actually.

@bitinn
Copy link
Collaborator

bitinn commented Jul 12, 2015

Good to know, I will close this for now.

@bitinn bitinn closed this as completed Jul 12, 2015
@F21
Copy link

F21 commented Feb 1, 2016

Just a quick note, @vyorkin's original example has a mistake. It should be:

let fetch = require('node-fetch');

(async () => {
  try {
    // request
    let response = await fetch('http://localhost:8088/api/v1/hooy');
    // parsing
    let data = await response.json();
    console.log('data: ', data);
  } catch (error) {
    console.log('error: ', error);
  }
})(); // <--------- parenthesis should be like this.

@vyorkin
Copy link
Author

vyorkin commented Feb 1, 2016

@F21 yeah, right, good spot!

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

No branches or pull requests

4 participants