GithubHelp home page GithubHelp logo

Comments (22)

domenic avatar domenic commented on May 13, 2024

I don't think this syntax carries its weight. Compare with the current proposal:

async function double(num) {
  num = await num;
  return num * 2;
}

Adding a whole new concept to the language (keyword prefixes to arguments which modify their behavior) just to save yourself a bit of typing is IMO not worth it.

from proposal-async-await.

knpwrs avatar knpwrs commented on May 13, 2024

@domenic Is there currently a way to await on more than one value? I've added another example above as such:

async function multiply(first, second) {
  var [first, second] = await Promise.all([first, second]);
  return first * second;
}

Though I suppose that's not actually any different than this:

async function multiply(first, second) {
  first = await first;
  second = await second;
  return first * second;
}

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024

Yes, or [first, second] = [await first, await second].

from proposal-async-await.

knpwrs avatar knpwrs commented on May 13, 2024

Ah, that makes sense. I was trying [first, second] = await [first, second]. Makes much more sense the real way.

from proposal-async-await.

benjamingr avatar benjamingr commented on May 13, 2024

@KenPowers this can be done with a decorator: http://babeljs.io/repl/#?

function argwait(fn){
  return function(){
    return Promise.all(arguments).then(args => fn.apply(this, args));
  };
}


let foo = argwait(async function (x, y){
  console.log(x, y);
});

let delay = (v, ms) => new Promise(r => setTimeout(r.bind(null, v), ms));

foo(Promise.resolve(5), delay(4, 1000));

http://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=function%20argwait(fn)%7B%0A%20%20return%20function()%7B%0A%20%20%20%20return%20Promise.all(arguments).then(args%20%3D%3E%20fn.apply(this%2C%20args))%3B%0A%20%20%7D%3B%0A%7D%0A%0A%0Alet%20foo%20%3D%20argwait(async%20function%20(x%2C%20y)%7B%0A%20%20console.log(x%2C%20y)%3B%0A%7D)%3B%0A%0Alet%20delay%20%3D%20(v%2C%20ms)%20%3D%3E%20new%20Promise(r%20%3D%3E%20setTimeout(r.bind(null%2C%20v)%2C%20ms))%3B%0A%0Afoo(Promise.resolve(5)%2C%20delay(4%2C%201000))%3B

from proposal-async-await.

bterlson avatar bterlson commented on May 13, 2024

While a good suggestion, I don't think this is something I want to include in the proposal at this time.

from proposal-async-await.

knpwrs avatar knpwrs commented on May 13, 2024

@domenic Just curious, my understanding was that [await foo(), await bar()] should call foo() immediately followed by bar() and wait for both results. This doesn't appear to work in Babel ( http://l.knpw.rs/1St1HTg ). The total wait time is >= the sum of both delays. Is this a bug in Babel or a bug in my understanding?

from proposal-async-await.

knpwrs avatar knpwrs commented on May 13, 2024

Just to be clear, this would do what I'm expecting:

const [foo, bar] = await Promise.all([getFoo(), getBar()]).

Assuming get{Foo,Bar} return Promises.

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024

@knpwrs that's a bug in your understanding. JavScript statements are executed right to left, so the await will happen on foo()s return value, and then the call to bar() will happen and its return value will be awaited.

This will also work:

const [fooP, barP] = [getFoo(), getBar()]; // this could also be two lines if you want
const [foo, bar] = [await fooP, await barP];

from proposal-async-await.

qfox avatar qfox commented on May 13, 2024

Shortly:

// x, y, z are some promises.
[await x, await y, await z]; // serial execution
await Promise.all([x, y, z]); // parallel execution

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024
[await x, await y, await z]; // serial execution

This is incorrect. That will be in parallel.

from proposal-async-await.

knpwrs avatar knpwrs commented on May 13, 2024

In parallel because they are values where presumably there is already an action in progress as opposed to a function call. Although I suppose if you wanted to get silly you could define getters on window for x, y, z that do something and then return a Promise. That would be serial, correct?

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024

Correct.

from proposal-async-await.

qfox avatar qfox commented on May 13, 2024

Let's assume foo and bar returning promises.
What's the difference between [await foo(), await bar()] and [await x, await y]? Why the first one will be executed serially? Sorry for dumb question.

from proposal-async-await.

inikulin avatar inikulin commented on May 13, 2024

Shortly =):

// x, y, z are some promises.
// fnX, fnY, fnZ functions that returns promises
[await fnX(), await fnY(), await fnZ()]; // serial execution
[await x, await y, await z]; // parallel execution
await Promise.all([x, y, z]); // parallel execution

from proposal-async-await.

inikulin avatar inikulin commented on May 13, 2024

@zxqfox In first case bar will not be executed until await foo() will not be resolved. [await x, await y] - promises are already executing in parallel.

from proposal-async-await.

qfox avatar qfox commented on May 13, 2024

Thanks. fnX = () => x

from proposal-async-await.

inikulin avatar inikulin commented on May 13, 2024

Right

from proposal-async-await.

qfox avatar qfox commented on May 13, 2024

I mean ;-):

[await () => x, await () => y, await () => z]; // serial execution

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024

That's still parallel, since x y and z already exist before the awaits happen.

from proposal-async-await.

domenic avatar domenic commented on May 13, 2024

Actually that's just a no-op as awaiting functions will do nothing interesting.

Unsubscribing from this thread.

from proposal-async-await.

benjamingr avatar benjamingr commented on May 13, 2024

Please keep support questions at support channels like Stack Overflow, IRC, chats etc.

When you ask a support Q here - it literally emails over 70 people.

If you have a concrete proposal or a suggestion, please open a separate thread.

from proposal-async-await.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.