gen-thread/example.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-27 16:03:06 +03:00
var gen = require('./gen-thread.js');
function* test(thread)
{
console.log('start');
console.log([ 'next', yield setTimeout(function() { thread('zhopa', 123); }, 500) ]);
var args = yield gen.runParallel([
function*(thread)
{
yield setTimeout(function() { thread('callback 1'); }, 500);
return 'result 1';
},
function*(thread)
{
yield setTimeout(function() { thread('callback 2'); }, 500);
return 'result 2';
}
], thread);
console.log('abc');
console.log(args);
return 'result';
}
2016-07-17 00:35:35 +03:00
function* test_throttle(thread)
{
yield thread.throttle(5);
console.log('at most 5');
yield setTimeout(thread, 1000);
2016-07-19 13:38:35 +03:00
console.log('continue in another generator');
2016-07-20 13:54:51 +03:00
yield gen.run(other_gen, null, thread);
2016-07-19 13:38:35 +03:00
}
function* other_gen(thread)
{
yield setTimeout(thread, 1000);
console.log('finished in another generator');
2016-07-17 00:35:35 +03:00
}
function* test_throw(thread)
{
var cb = thread.errorfirst();
try
{
yield setTimeout(function() { cb(new Error()); }, 500);
}
catch (e)
{
console.log('Catched '+e.stack);
}
console.log(yield setTimeout(thread.cb(), 500));
console.log('sleep');
console.log(yield setTimeout(thread.cb(), 500));
console.log('continue');
}
//gen.run(test, null, function(result) { console.log(result); });
//for (var i = 0; i < 15; i++) gen.run(test_throttle);
gen.run(test_throw);