async/await KeywordsIt simplifies working with asynchronous code.When a function is defined as async it returns a promise i.e the return value is wrapped as a promise.
The await keyword can only be used within a async function.The control waits until the promise is settled.
function fx() {
return new Promise(resolve => {
setTimeout(() => {
resolve(‘resolved’);
}, 1000);
});
}
async function asyncFx() {
console.log(‘calling’);
const result = await fx();
console.log(result);
}
asyncFx();
In the above code the control waits inside asyncFx() function until fx() function is completed. When the fx() function returns a promise the value is captured and processed inside the asyncFx() function.
Object.entries() function
It converts an object into an 2D array with key – value as pairs within the array.
let city = {
“delhi”:1000,
“mumbai”:2000,
“bangalore”:1500
};
console.log(Object.entries(city));
for (let [k,v] of Object.entries(city))
console.log(k,v);
In the above code the city object is converted to 2D array. The array can be iterated using the for..of loop.
Object.values() function
It extracts values from an object and returns an array composed of those values.
let city = {
“delhi”:1000,
“mumbai”:2000,
“bangalore”:1500
};
console.log(Object.values(city));
In the above code the object is converted to an array containing only values i.e the array does not have any keys.
Object.getOwnPropertyDescriptors()
It returns all the details of a property. The details that are returned for a property are
- value
- enumerable
- writable
- configurable
let city = {
“delhi”:1000,
“mumbai”:2000,
“bangalore”:1500
};
console.log(Object.getOwnPropertyDescriptors(city));
In the above code all the property details are returns with their status.
padStart() and padEnd() functions
These functions are used for padding a value at the start and at the end.
console.log(“ram”.padStart(7,0));
console.log(“ram”.padEnd(7,0));
In the above code is padded at the start and end 7 times with 0.
Complete Code Listing
function fx() {
return new Promise(resolve => {
setTimeout(() => {
resolve(‘resolved’);
}, 1000);
});
}
async function asyncFx() {
console.log(‘calling’);
const result = await fx();
console.log(result);
}
asyncFx();
let city = {
“delhi”:1000,
“mumbai”:2000,
“bangalore”:1500
};
console.log(Object.entries(city));
for (let [k,v] of Object.entries(city))
console.log(k,v);
console.log(Object.values(city));
console.log(Object.getOwnPropertyDescriptors(city));
console.log(“ram”.padStart(7,0));
console.log(“ram”.padEnd(7,0));
Source: hashnode.com