pheuter / essential-react

A minimal skeleton for building testable React apps using Babel
MIT License
2.02k stars 131 forks source link

ES7 await #19

Open therealmarv opened 9 years ago

therealmarv commented 9 years ago

I'm no ES7 expert. Somebody with more knowledge may review it.

It looks much nicer for me.

Credits go to @lukehoban

mathbruyen commented 9 years ago

Minor point but you may use await* instead of await Promise.all(...). The spec proposal feels a bit unsure about it so it may change, but it works with babel.

carlosvillu commented 9 years ago

@mathbruyen is right. With await* you follow the spec and is easier to read.

lukehoban commented 9 years ago

My understanding is that await* is not part of the ES7 timeframe async/await proposal. At least it doesn't appear to be in the spec draft that @bterlson is writing.

carlosvillu commented 9 years ago

I am working in something that is using that.

https://github.com/carlosvillu-com/graphql-basic/blob/master/pokemon-client.js#L11

What do you think ?!

2015-07-10 23:47 GMT+02:00 Luke Hoban notifications@github.com:

My understanding is that await* is not part of the ES7 timeframe async/await proposal. At least it doesn't appear to be in the spec draft https://tc39.github.io/ecmascript-asyncawait/ that @bterlson https://github.com/bterlson is writing.

— Reply to this email directly or view it on GitHub https://github.com/pheuter/essential-react/pull/19#issuecomment-120536990 .

mathbruyen commented 9 years ago

In your case the descriptions method actually synchronously returns an array of promises, while await* will make it return a single promise once all in the array are fulfilled (or any is rejected). If you want to see the difference in action, run the following (tried with babel 5.1.10) with babel-node --stage 1 test.js, then remove the * in `foo and re-run it.

function wait(i) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log('Resolve', i);
      resolve(i * 2);
    }, i * 1000);
  });
}
async function foo() {
  return await* [1, 2, 3, 4].map(wait);
}
foo().then(arr => console.log(arr));

With the * it displays fist Resolve X each second and then the doubled array. Without it, an array of promises is displayed and later Resolve X appear.

carlosvillu commented 9 years ago

You are right! ... Thank for the info. But I think that in my particular case, is better use it without "*". I have to fine tune my es6 knowledge.

2015-07-13 11:51 GMT+02:00 Mathieu Bruyen notifications@github.com:

In your case the descriptions method actually synchronously returns an array of promises, while await* will make it return a single promise once all in the array are fulfilled (or any is rejected). If you want to see the difference in action, run the following (tried with babel 5.1.10) with babel-node --stage 1 test.js, then remove the * in `foo and re-run it.

function wait(i) { return new Promise(resolve => { setTimeout(() => { console.log('Resolve', i); resolve(i * 2); }, i * 1000); }); } async function foo() { return await* [1, 2, 3, 4].map(wait); } foo().then(arr => console.log(arr));

With the * it displays fist Resolve X each second and then the doubled array. Without it, an array of promises is displayed and later Resolve X appear.

— Reply to this email directly or view it on GitHub https://github.com/pheuter/essential-react/pull/19#issuecomment-120876049 .