Suppose I have an async function * ()
( setup here ) like this:
const f = async function * () {
yield * [ 1, 2, 3 ];
};
I can gather the results like this:
const xs = [];
for await (const x of f()) {
xs.push(x);
}
But can I use the ...
syntax to make this more compact?
Something like:
const xs = await f(); // xs = [ 1, 2, 3 ]
The best you can do is put it into a function:
const toArray = async f => {
const xs = [];
for await (const x of f) {
xs.push(x);
}
return xs;
};
Usage:
// In an async context
const xs = await toArray(f());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.