Async GeneratorsThey combine the capabilities of
- Generator function
- Async function
The programmer can use await and yield keyword together. The function returns a promise object. The syntax is :
async function* () { yield await Promise.resolve(1); }
The returned promise object contains value in form of an object with attributes value and done. e.g {value: 1, done: false}
async function* fx() {
yield await Promise.resolve(1);
yield await Promise.resolve(2);
yield await Promise.resolve(3);
}
let genobj = fx();
genobj.next().then(v => console.log(v));
genobj.next().then(v => console.log(v.value));
genobj.next().then(v => console.log(v.value));
genobj.next().then(v => console.log(v.value));
In the above code an async generator function is created. The function returns a generator object. When next() is called on the generator object and it waits until the promise is settled. After the promise settles it is returned which is processed using the then() function.
Loop for..await
The loop for..await is used for iterating async iterables.
async function* fx() {
yield await Promise.resolve(1);
yield await Promise.resolve(2);
yield await Promise.resolve(3);
}
async function forawait() {
for await (let obj of fx()) {
console.log(“for await”, obj);
}
}
forawait();
In the above code the function forwait is asynchronous, it uses the for..await loop to iterate over async generator. Each iteration of for..await returns a value yielded by the generator.
Rest operator with Objects
The rest operator(…) can be used with objects to extract remaining properties into an object.
let obj = {
name:”ram”,
age:30,
salary:1000
};
let {name, …rest} = obj;
let {age, …rest2} = obj;
console.log(name,rest);
console.log(age,rest2);
In the above code the object obj’s remaining properties are extracted into rest and rest2 respectively.
Spread operator with Objects
It can be used for creating
The spread operator extracts the properties of the host object.
let obj = {
name:”ram”,
age:30,
salary:1000
};
let obj2 = {
name2:”ram”,
age2:30,
salary2:1000
};
let obj3 = {…obj};
let obj4 = {…obj, …obj2};
console.log(“obj3”, obj3);
console.log(“obj4”, obj4);
In the above code the object obj is shallow copied to obj3 and object obj, obj2 are merged into object obj4.
Complete Code Listing
async function* fx() {
yield await Promise.resolve(1);
yield await Promise.resolve(2);
yield await Promise.resolve(3);
}
let genobj = fx();
genobj.next().then(v => console.log(v));
genobj.next().then(v => console.log(v.value));
genobj.next().then(v => console.log(v.value));
genobj.next().then(v => console.log(v.value));
async function forawait() {
for await (let obj of fx()) {
console.log(“for await”, obj);
}
}
forawait();
let obj = {
name:”ram”,
age:30,
salary:1000
};
let {name, …rest} = obj;
let {age, …rest2} = obj;
console.log(name,rest);
console.log(age,rest2);
let obj2 = {
name2:”ram”,
age2:30,
salary2:1000
};
let obj3 = {…obj};
let obj4 = {…obj, …obj2};
console.log(“obj3”, obj3);
console.log(“obj4”, obj4);
Source: hashnode.com