async / await in JavaScript - What, Why and How - Fun Fun Function

Fun Fun Function · 254K views · 2017 · 24:00

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions from people confused by Promises and asynchronous code, as an introduction to async/await.

Recommended by 7 people across 6 different months, 2017 to 2020.

What it is

A 24-minute episode of the Fun Fun Function series in which mpj explains JavaScript's async and await keywords, showing how they pause an async function until a Promise resolves so asynchronous code reads like synchronous code. It builds on his earlier Promises and map videos and includes live code snippets.

What people said

Recommends Ramon Gebben · 2017-08 · Stack Overflow · on “How to use an axios response in another axios get in React JS?”

One way to achieve this would be to just call inside the then of your first GET and use a template string. Like so: const getMyStuff = new Promise((resolve, reject) => { axios.get(' [LINK] ', headers) .then((response) => { const versionID = response.data.map(({ id }) => id); axios.all([ axios.get(` [LINK] = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), axios.get(` [LINK] IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), axios.get(` [LINK] IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), ]) .then(resolve) .catch(reject) }) .catch(reject); });

Recommends Bharathvaj Ganesan · 2017-08 · Stack Overflow · on “How do I set user_id automatically in MySQL using Nodejs?”

Use MYSQL AUTO_INCREMENT feature to create a new user_id for each new user (i.e each new row) while inserting as follows CREATE TABLE USERS( user_id INT NOT NULL AUTO_INCREMENT); cheers For Callbacks and Promises checkout this video [LINK] and for latest await /async feature this video [this video]

Recommends chipadmin · 2018-04 · Stack Overflow · on “How do I make a NodeJS application run forever (not the forever module)”

I've found the solution to this thanks to Paulpro [LINK] telling me I was on the right track. First off I switched to using mongoist which support promises and then I'm using the new async and await syntax to get the desired results: const snooze = ms => new Promise(resolve => setTimeout(resolve, ms)); getToProcessCount(); async function getToProcessCount(){ while(true){ const datacnt = await db.queue.count({processed: 0}); console.log(datacnt); if(datacnt == 0){ console.log("Nothing to do, sleep for a while"); await snooze(2000); console.log("I just woke up"); } else{ console.log("Do some processing here"); } } } The async function allows this type of function to pause its execution and wai

Recommends DauleDK · 2018-06 · Stack Overflow · on “null Firestore whit reactive form”

When you reset an reactive form, all values are set to null. So the result you see is what I would expect. But the problem is that you don't take into consideration the asynchronous nature, of the operation to firestore. To get the result you are looking for, you should refactor to this: async agregarDomiciliario() { await this.fs.addDomiciliario(this.forma.value); // Wait for firestore, before resetting the form this.form.resetForm({ fecha: firebase.firestore.FieldValue.serverTimestamp(), estado: false }); // Show the snackbar this.snackBar.open(this.message, this.action, { duration: 5000, }); } addDomiciliario(domiciliario){ return this.afs.collection('domiciliarios').add(domiciliario); }

Recommends DauleDK · 2018-06 · Stack Overflow · on “Delete user and all his documents Firestore”

The solution is to create an async function. You mentioned that you are new to programming, and in the beginning it can be difficult to understand promises and async/await. I would recommend that you watch this video [this video] for an introduction. You can solve it in 2 ways. Async, jut add the async word: async deleteAccount() { Create a promise chain (try it out for practice): deleteAccount() { firebase.firestore.QuerySnapshot = await this.afs.collection('houses', ref => ref.where('email', '==', this.afAuth.auth.currentUser.email)).ref.get() .then(qry => { const batch = this.afs.firestore.batch(); qry.forEach(doc => batch.delete(doc.ref)); return batch.commit(); }) .then(() => console.log('do

Recommends r-sede · 2018-07 · Stack Overflow · on “use ajax get value outside ajaxstatement”

this a classic asynchrone problem, you have to use "data" inside the done callback. read thing about promise [LINK] , async / await [this video] can help you i think

Recommends AndreasM_DK · 2020-03 · Stack Overflow · on “Protractor not wait for https.request callback”

The function above is an async and when you make your function async, then you should use the await keyword in front of the https.request or return it like this. return https.request... And another thing that could lead to problems are... https.request is making a direct HTTP request, without using a browser broser.getTitle() is using the browser to interact with the web page. Be aware - browser uses the browser, and http.request uses direct HTTP from node.js - these are two different things. And it will lead to unpredictable things to mix them. So consider if you want to "test as a user" and then use the browser, or if you want to do the fastest possible test and "test as a website or javas

Recommends Alejandro A. · 2020-12 · Stack Overflow · on “How can I get the properties of a nested object with strange characters such as []?”

The reason why you cannot access your data at that point of the runtime is because response response.json() returns a Promise, not the actual data, so one option is to await as pointed out by @Keith above, or use then() . If you want to dig deeper into promises, async/await, and even as historical learning, the callback(err, res) approach, I suggest you to check out: async/await [this video] promises [LINK]

Cites webwelten · 2018-10 · Stack Overflow · on “Shorthand promise chaining”

sure you can shorten your code but first of all there are a few misconceptions about promises. In the end I'm giving you a solution and another short version. But I would recommend you to really try and understand promises in detail before just using that code. It's an important part of javascript. [LINK] [LINK] 1. Try .then(user.verifyPassword(req.body.password)) then is supposed to take a function. But you're passing the result of verifyPassword which is the Promise object. Your expression becomes then(null) so that the previous result of user.exists() passes through. Instead you can do the following: user.exists() .then(() => user.verifyPassword(req.body.password)) 2. Try Obviously the wr