const generator = (function* () {
for (let i = -2e9; i < 2e9; i++) {
yield console.log(i);
}
}());
while(!generator.next().done);
var i = 0;
setInterval(function() {
document.writeln(i++);
}, 666);
function xxx(i) {
document.writeln(i);
setTimeout(xxx, 666, i + 1);
}
xxx(0);
const createPromiseLoop = promises => {
let index = 0;
const attach = (...args) => {
promises[index++](...args)
.then((...args) => attach(...args));
index %= promises.length;
};
return (...args) => {
attach(...args);
};
};
const sleep = (duration = 0) => new Promise(resolve => setTimeout(resolve, duration));
const loop = createPromiseLoop([
(number) => new Promise(async resolve => {
await sleep(100);
console.log(`Number plus one. Received: ${number}`);
resolve(number + 1);
}),
(number) => new Promise(async resolve => {
await sleep(100);
console.log(`Number multiply two. Received: ${number}`);
resolve(number * 2);
}),
]);
loop(1);
/*
'Number plus one. Received: 1'
'Number multiply two. Received: 2'
'Number plus one. Received: 4'
'Number multiply two. Received: 5'
'Number plus one. Received: 10'
'Number multiply two. Received: 11'
'Number plus one. Received: 22'
'Number multiply two. Received: 23'
'Number plus one. Received: 46'
'Number multiply two. Received: 47'
'Number plus one. Received: 94'
'Number multiply two. Received: 95'
'Number plus one. Received: 190'
'Number multiply two. Received: 191'
'Number plus one. Received: 382'
'Number multiply two. Received: 383'
'Number plus one. Received: 766'
'Number multiply two. Received: 767'
'Number plus one. Received: 1534'
'Number multiply two. Received: 1535'
'Number plus one. Received: 3070'
'Number multiply two. Received: 3071'
'Number plus one. Received: 6142'
'Number multiply two. Received: 6143'
'Number plus one. Received: 12286'
'Number multiply two. Received: 12287'
'Number plus one. Received: 24574'
*/