Comments (22)
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.
@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.
Yes, or [first, second] = [await first, await second]
.
from proposal-async-await.
Ah, that makes sense. I was trying [first, second] = await [first, second]
. Makes much more sense the real way.
from proposal-async-await.
@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));
from proposal-async-await.
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.
@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.
Just to be clear, this would do what I'm expecting:
const [foo, bar] = await Promise.all([getFoo(), getBar()]).
Assuming get{Foo,Bar}
return Promise
s.
from proposal-async-await.
@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.
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.
[await x, await y, await z]; // serial execution
This is incorrect. That will be in parallel.
from proposal-async-await.
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.
Correct.
from proposal-async-await.
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.
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.
@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.
Thanks. fnX = () => x
from proposal-async-await.
Right
from proposal-async-await.
I mean ;-):
[await () => x, await () => y, await () => z]; // serial execution
from proposal-async-await.
That's still parallel, since x y and z already exist before the awaits happen.
from proposal-async-await.
Actually that's just a no-op as awaiting functions will do nothing interesting.
Unsubscribing from this thread.
from proposal-async-await.
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)
- How do you yield* with async/await? HOT 5
- Async arrow functions don't work with default destructed parameters. HOT 1
- server.asyncawait.js uses `await*` syntax
- Question: 'await' is a keyword? HOT 3
- Proposal: parallelize consecutive awaits HOT 4
- Play sounds when messages are received by both admins and users HOT 1
- Clarify the relationship between loops and async HOT 5
- Error in javascript discord bot HOT 1
- A question about async await with native promise. HOT 7
- Spec example needs a tiny fix. HOT 2
- await in spawn HOT 1
- New syntax HOT 1
- > > Btw, what's the case of exporting async function atm? Can't imagine a single one, even if it's a valid syntax. HOT 2
- node google spreadsheet i want to fetch a particular cell HOT 1
- async
- Forbidden Extensions (16.2) should apply to async functions, as they do to Generators HOT 1
- NOTE 1 for Async Function Definitions says different things than rules HOT 1
- Update of "State of this proposal" pharagraph in https://tc39.github.io/ecmascript-asyncawait/ HOT 8
- Throw if ClassHeritage is an async function! HOT 1
- Exceptions swallowed by design? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-async-await.