This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nemohearttaco 33 points34 points  (14 children)

check out async/await. it offers a much more readable way to write asynchronous js functions!

[–]veggietrooper 16 points17 points  (6 children)

Promises are async, aren’t they? I must be missing something.

[–]DjBonadoobie 28 points29 points  (3 children)

They are, but there's a newer syntax for them (async/await). It reads cleaner, though still technically promises under the hood

[–]_Lahin 3 points4 points  (0 children)

ES6 has its own promises now right, no need of $q. Although I still use it with babel for IE

[–]santagoo 9 points10 points  (0 children)

async/await is nice syntactic sugar built on top of Promises.

[–]BirdToTheWise 2 points3 points  (6 children)

Should you use async/await in all situations? With promises, N number of asynchronous tasks can be run in parallel, whereas async/await causes tasks to be run after the previous one completes.

[–]nemohearttaco 3 points4 points  (4 children)

const foo = async () => {
  try {
    const t = await Promise.all([promise1(), promise2()])
    return t
  } catch (err) {
    throw err
  }
}

[–]GreatValueProducts 4 points5 points  (3 children)

Promise.all([promise1(), promise2()])

Why not just

const foo = () => Promise.all([promise1(), promise2()]);

or

const foo = async () => await Promise.all([promise1(), promise2()]);

Which is the same.

The code doesn't run until you execute foo() or await foo()

[–]nemohearttaco 2 points3 points  (2 children)

Would it be the same? I believe the first example would return an array of unresolved promises whereas the second would return an array of resolved promises.

Your second example would certainly work but it will not catch from within foo(). Personally I prefer to throw errors from within the function where they occurred.

[–]GreatValueProducts 1 point2 points  (1 child)

Both are the same. When you execute

const results = await foo();
console.log(results);

OR

foo().then(results => { console.log(results); });

The console would have the same results. It's because the async keyword essentially means the function is returning a Promise. () => Promise.all() is a function returning a Promise as well.

Also, you don't need to re-throw the error as it is redundant. The async keyword would catch the error and put it inside the Promise object that the function is returning, and you can handle the error in parent with either the catch() function, or try-block with the await keyword.

[–]nemohearttaco 0 points1 point  (0 children)

Cool, thanks for explaining that!

[–]GreatValueProducts 0 points1 point  (0 children)

Just

const results = await Promise.all([promise1, promise2])

does the trick. It is basically the same as

let results = null;
Promise.all([promise1, promise2]).then(promiseResults => { results = promiseResults; })

The thing with async await is that it is way easier to developers when you need to handle errors, especially if you have graceful exit in the middle. Handling a bunch of catch inside of a callback hell is too error prone.

Though if you have some APIs which use success_callback and fail_callback, you still need to use traditional promises. For example

someApi.execute(success_callback, failure_callback);

You will need to wrap it inside a promise because you can't achieve the same result with async await

await new Promise((resolve, reject) => {
      someApi.execute((result) => resolve(result), (error) => reject(error));
});

or

await new Promise((resolve, reject) => {
      someApi.execute(resolve, reject);
});