问题:
是否可以像使用var promise = document.querySelector('audio').play();
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
}).catch(error => {
// Autoplay was prevented.
// Show a "Play" button so that user can start playback.
});
}
一样将两个生成器组合为一个生成器?
compose
答案 0 :(得分:1)
首先,您的type
函数实际上可以被缩短。其次,这是您的composeGenerator
从左到右的一种可能的实现方式:
function * type (iterable) {
yield * iterable;
}
const bool = type([true, false]);
const str = type([
'',
undefined,
'Lorem',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'????'
]);
function * composeGenerator (...iterables) {
for (const iterable of iterables) yield * iterable;
}
const input = composeGenerator(str, bool);
for (const i of input) {
console.log(i);
}
答案 1 :(得分:0)
我们迭代所有生成器,直到它们产生一个值
function* type(vals) {
for(const v of vals) {
yield v;
}
}
const bool = type([true, false]);
const str = type([
'',
undefined,
'Lorem',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'????'
]);
function *composeGenerator(...iterators) {
for (const iterator of iterators) {
for (const value of iterator ) {
yield value;
}
}
}
const input = composeGenerator(bool,str);
for (const i of input) {
console.log(i);
}