Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

How do you yield* with async/await? #100

Closed
Fishrock123 opened this issue Jul 4, 2016 · 5 comments
Closed

How do you yield* with async/await? #100

Fishrock123 opened this issue Jul 4, 2016 · 5 comments

Comments

@Fishrock123
Copy link

Hi there, I was looking to move my CLI tool org-labels over to async/await as an experiment, using node+v8-5.2 & --harmony_async_await.

I've noticed there does not immediately appear to be a replacement for yield*, as with the co library.

That is, it is unclear what you need to do to wait for all awaits to resolve within an async function...

@Fishrock123
Copy link
Author

Fishrock123 commented Jul 4, 2016

@kittens Given the following: (They deleted their comment...)

async function a() {
  await x; // must wait for this
  await y; // AND this
}

async function b() {
  await a(); // previously `yield* a()`
}

do you mean await Promise.all(a())? Will the async function with multiple awaits return an array?

Always or only when multiple exist/are run?

@Fishrock123 Fishrock123 changed the title How to do yield* with async/await? How do you yield* with async/await? Jul 4, 2016
@ilkkao
Copy link

ilkkao commented Jul 4, 2016

Similar issue: #61

@raymond-h
Copy link

raymond-h commented Jul 4, 2016

Async functions with multiple awaits still only return a single promise, which is resolved once the function has finished. await makes it wait for a given promise to resolve before the function is continued.

With multiple awaits, it'll wait until the first promise is resolved, then the second promise, then the third promise, etc, until the end of the function has been reached. At that point, the promise that the function as a whole returned is resolved.

In your example, when b is called, it'll call a and get a promise that it waits for to get resolved. The function a, in turn, waits for promise x to be resolved, then promise y. Once that's done, a returns, at which point the promise that was gotten by b is resolved.

@raymond-h
Copy link

raymond-h commented Jul 4, 2016

Because an async function that awaits on promises itself returns a promise that is resolved when the async function return, await works for awaiting the result of other async functions too out of the box, so it is a non-issue essentially.

@Fishrock123
Copy link
Author

Hmm, ok. Looks/sounds like this is the default behavior. In a bit of hindsight that makes sense.

That being said I am running into problems related to this, but not really this issue.

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

No branches or pull requests

3 participants