The Async Await Episode I Promised

Fireship · 1.2M views · 2018 · 12:04

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about promises, async/await and asynchronous control flow, as a quick beginner-friendly explainer.

Recommended by 10 people across 10 different months, 2018 to 2022.

What it is

A roughly 12-minute Fireship episode that teaches JavaScript async/await as a cleaner way to write Promise-based code, with accompanying GitHub examples and pro tips. It links out to Jake Archibald's talk on the event loop for deeper background.

What people said

Recommends Florian Ludewig · 2018-10 · Stack Overflow · on “how can i print result array in node js outside mongoOp find query?”

I would make use of async and await router.get('route', (req, res) => { // ... users.find(query, (err, d) => { try { // ... var results = [] for (var data of array) { const result = await fetchUser(data) results.push(result) } console.log(results) } catch (err) { console.log('some error occured', err) } }) }) async function fetchUser(id) { return new Promise((resolve, reject) => { users.find({ _id: id }, (err, data) => { return err ? reject(err) : resolve(data) }) }) } If you're not that familiar with async and await I would recommend this video [this video]

Recommends Webbanditten · 2019-01 · Stack Overflow · on “Accessing login credentials and show them on the drawer component”

The warning your're getting is because your not handling your promise. You need to resolve your promise properly. Take a look at [this video] to learn more. You would need to return userInfo to use it anywhere else.

Recommends Fernando Luca De Tena · 2019-04 · Stack Overflow · on “Async/ await in Firebase Cloud Function says Error Expression has type `void`”

just in case someone comes after. As Ron stated before, you shouldn't use async/await inside a traditional for loop if the second call doesn't need the value of the first, as you will increase the run time of the code. Furthermore you can't use async/await inside of a forEach=> or a map=> loop, as it won't stop and wait for the promise to resolve. The most efficient way is to use Promise.all([]) . Here I leave a great youtube video of a crack that explains async/await and Promises => [this video] As to one of the questions in the comments by DarkHorse: But all I want is for getTeamPlayers() to execute and write to the database so I don't know why I need to return anything. In Firebase Functions a

Recommends jaloplo · 2019-06 · Stack Overflow · on “What are the advantages of using async/await?”

@Bergi afaik, Promise.all method allows to run more than one task at a time. It will resolve when the longest task ends so you will not waste time running synchronously. Let me know if I'm wrong. I followed this video as the master for learning async/await [this video]

Recommends Vl4dimyr · 2019-10 · Stack Overflow · on “How to change a single value inside of an object inside of an array in a mongoose model and save it?”

Ok for then/catch async/await I suggest watching this: [this video] and for the code please try adding `await taskFromDB.save()` before `await userFromDB.save()` maybe you don't even need the `await userFromDB.save()` after this

Recommends Jeff Rush · 2020-01 · Stack Overflow · on “Google Sheets Node.js Quickstart Guide: How to return the rows array returned from API into a variable”

@MatthewWolman If you want to create global variables, just create global.js and request it in index.js; If you are getting blank output this is because you are calling for results too early, I suppose you know how promises and async/await work, in case you don't know here is an introduction to promises [LINK] and youtube video about Async Await [this video]

Recommends Bjørn Reemer · 2021-01 · Stack Overflow · on “Best practice for nesting ajax calls”

Given your work case, you could consider the following method: artistForm.submit(async (e) => { e.preventDefault(); let inputSearchQuery = artistInput.val(); let searchQuery = encodeURI(inputSearchQuery); let artist = await $.ajax({ url: `${baseUrl}search?q=${searchQuery}&type=artist`, type: 'GET', datatype: 'json', headers: { 'Authorization': 'Bearer ' + accessToken } }); const artistId = artists.items[0].id; let topTracks = await $.ajax({ url: `${baseUrl}artists/${artistId}/top-tracks?country=CZ`, type: 'GET', datatype: 'json', headers: { 'Authorization': 'Bearer ' + accessToken } }); let relatedArtists = await $.ajax({ url: `${baseUrl}artists/${artistId}/related-artists`, type: 'GET', dat

Recommends Itai Pendler · 2021-07 · Stack Overflow · on “Array push not working inside promise then”

The problem is that the then callback happens asynchronously, meaning that there may (and probably will) be a delay between when the code inside the then callback is executed and the rest of the code outside of it. If you want to depend on an async function, be sure to read about async/await. The await keyword, when put before an async function, will halt the program until the promise is resolved. For more see here [this video] and here [LINK]

Recommends Youssouf Oumar · 2022-05 · Stack Overflow · on “Why does Mongoose updateMany() only work when using .then”

I learned from the post of @Mats below that updateMany does not work without .then because it returns a Mongoose Query Object, not the Promise [LINK] meant to update the data. They have a .then() function for co [LINK] and async/await as a convenience. If you need a fully-fledged promise, use the .exec() function. Read the post of @Mats below for more. My answer when I thought it returns the Promise: Actually, it updates the data, but for you to have the updated data, you need to wait for it, as a Promise is a task that starts and ends later. .then lets you say, I wanna know when the task I asked for is done. To understand Promise in JavaScript you could watch this [this video] , or use the above

Recommends Ali Osman · 2022-11 · Stack Overflow · on “How to call variable from async function”

I believe this is because the function is async so it finishes setting the value after the log outside, One solution is to return the value needed in the variable directly and await it like so const LM35TOTALRES = await (db.collection("LM35").get()).size This occurs due event loop scheduling you can find some good examples in this video [this video] about min 2 or so