async await javascript
JavaScript
/* Notes:
1. written like synchronous code
2. compatible with try/catch blocks
3. avoids chaining .then statements
4. async functions always return a promise
5. function pauses on each await expression
6. A non promise value is converted to
Promise.resolve(value) and then resolved
*/
// Syntax
// Function Declaration
async function myFunction(){
await ... // some code goes here
}
// Arrow Declaration
const myFunction2 = async () => {
await ... // some code goes here
}
// OBJECT METHODS
const obj = {
async getName() {
return fetch('https://www.example.com');
}
}
// IN A CLASS
class Obj {
// getters and setter CANNOT be async
async getResource {
return fetch('https://www.example.com');
}
}
async function showAvatar() {
// read our JSON
let response = await fetch('/article/promise-chaining/user.json');
let user = await response.json();
// read github user
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
let githubUser = await githubResponse.json();
// show the avatar
let img = document.createElement('img');
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append(img);
// wait 3 seconds
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
img.remove();
return githubUser;
}
showAvatar();await async wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}async function f() {
try {
let response = await fetch('/no-user-here');
let user = await response.json();
} catch(err) {
// catches errors both in fetch and response.json
alert(err);
}
}
f();function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
//async function:
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
asyncCall();
Also in JavaScript:
- Title
- can we add new state property using setstate in react
- Category
- JavaScript
- Title
- get value of all checked boxes jquery
- Category
- JavaScript
- Title
- is object
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- how to add attribute to selected element in javascript
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- how to remove session data on tab close in redux\
- Category
- JavaScript
- Title
- change width in js
- Category
- JavaScript
- Title
- dotnet core ajax post of an ojject site:stackoverflow.com
- Category
- JavaScript
- Title
- array to set javascript
- Category
- JavaScript
- Title
- get all keys in json object
- Category
- JavaScript
- Title
- airbnb and eslint react native
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- how to read json file in python
- Category
- JavaScript
- Title
- Alert.alert closing expo
- Category
- JavaScript
- Title
- fetch get data js
- Category
- JavaScript
- Title
- alert user javascript
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- go to anchor jquery
- Category
- JavaScript
- Title
- firebase configuration on functions
- Category
- JavaScript
- Title
- how to ask input in javascript
- Category
- JavaScript
- Title
- get unique id angular
- Category
- JavaScript
- Title
- how to add button react native app.js
- Category
- JavaScript
- Title
- how to create a server in node js
- Category
- JavaScript
- Title
- caesar cipher javascript code
- Category
- JavaScript
- Title
- how to use aos in react
- Category
- JavaScript
- Title
- Appending the option element using jquery each function
- Category
- JavaScript
- Title
- go to page jquery
- Category
- JavaScript
- Title
- how to get variable in local storage in javascript
- Category
- JavaScript
- Title
- document ready
- Category
- JavaScript
- Title
- how to animate a flash in jquery
- Category
- JavaScript
- Title
- how to create request body javascript
- Category
- JavaScript
- Title
- chartjs lineTension
- Category
- JavaScript
- Title
- export aab react native
- Category
- JavaScript
- Title
- currying in javascript
- Category
- JavaScript
- Title
- how to add multiple comment in react
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- how to add up all the numbers in between 0 and that number
- Category
- JavaScript
- Title
- how to remove an object from array in react native
- Category
- JavaScript
- Title
- compare dates in js
- Category
- JavaScript
- Title
- how to append select option in jquery
- Category
- JavaScript
- Title
- how to close another browser tab with javascript
- Category
- JavaScript
- Title
- event listener on parent not on childjavascript
- Category
- JavaScript
- Title
- copy array javascript
- Category
- JavaScript
- Title
- daterangepicker set maxdate
- Category
- JavaScript
- Title
- javascript check if array is empty
- Category
- JavaScript
- Title
- get a element using name in jquery
- Category
- JavaScript
- Title
- Javascript clear canvas
- Category
- JavaScript
- Title
- is forEach sync js
- Category
- JavaScript
- Title
- filter out arrays js
- Category
- JavaScript
- Title
- dynamodb get all items nodejs
- Category
- JavaScript
- Title
- javascript call php function with parameters
- Category
- JavaScript
- Title
- firefox extension make ajax request
- Category
- JavaScript
- Title
- dataset js
- Category
- JavaScript
- Title
- hide element by name javascript
- Category
- JavaScript
- Title
- how to make a inventory in js
- Category
- JavaScript
- Title
- contains substring javascript
- Category
- JavaScript
- Title
- angular right click on link
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- how to use chart js in vue js
- Category
- JavaScript
- Title
- flutter vs react native
- Category
- JavaScript
- Title
- how to version a react app azure pipelines
- Category
- JavaScript
- Title
- How to check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- how to access a database in express
- Category
- JavaScript
- Title
- window location in react
- Category
- JavaScript
- Title
- how to get the size of the window in javascript
- Category
- JavaScript
- Title
- filter array objects javascript
- Category
- JavaScript
- Title
- how to generate random string in javascript
- Category
- JavaScript
- Title
- blur javascript
- Category
- JavaScript
- Title
- html inline javascript
- Category
- JavaScript
- Title
- forEach
- Category
- JavaScript
- Title
- format date js
- Category
- JavaScript
- Title
- arrow function
- Category
- JavaScript
- Title
- how to get a user input in js
- Category
- JavaScript
- Title
- clear scene\
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- binary search in js
- Category
- JavaScript
- Title
- javascript click button by id
- Category
- JavaScript
- Title
- datatable after. draw
- Category
- JavaScript
- Title
- check if localstorage key exists js
- Category
- JavaScript
- Title
- create dice game javascript
- Category
- JavaScript
- Title
- add element to array javascript
- Category
- JavaScript
- Title
- javascript add minutes to date
- Category
- JavaScript
- Title
- how to make item not dragable in react-sortablejs
- Category
- JavaScript
- Title
- how to make a vowel counter in javascript
- Category
- JavaScript
- Title
- for Loop continue
- Category
- JavaScript
- Title
- hide urls in .env in react app
- Category
- JavaScript
- Title
- function inside a class component react
- Category
- JavaScript
- Title
- get string of element
- Category
- JavaScript
- Title
- javascript base64 encode
- Category
- JavaScript
- Title
- Cannot set property 'innerHTML' of null
- Category
- JavaScript
- Title
- how to get the index of an array in javascript
- Category
- JavaScript
- Title
- ionic react use yarn
- Category
- JavaScript
- Title
- How to send JSON Web Token (JWT Token) as header with Postman and golang
- Category
- JavaScript
- Title
- create array of objects javascript
- Category
- JavaScript
- Title
- how to return the max and min of an array in javascript
- Category
- JavaScript
- Title
- javascript 1 + "1"
- Category
- JavaScript
- Title
- adding mui theme to index.js
- Category
- JavaScript
- Title
- array of in javascript
- Category
- JavaScript