2 Star 0 Fork 0

mirrors_vitalif/gen-thread

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example.js 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
var gen = require('./index.js');
function* test()
{
console.log('start');
// Should give no "Generator is already running" error
var v = yield gen.cb()('expected value');
console.log(v[0] === 'expected value');
var cb = gen.unsafe();
console.log([ 'next', yield setTimeout(function() { cb('zhopa', 123); }, 500) ]);
var args = yield gen.runParallel([
(function*()
{
var cb = gen.unsafe();
yield setTimeout(function() { cb('callback 1'); }, 500);
return 'result 1';
})(),
(function*()
{
var cb = gen.unsafe();
yield setTimeout(function() { cb('callback 2'); }, 500);
return 'result 2';
})()
], gen.cb());
console.log('abc');
console.log(args);
return 'result';
}
function* test_throttle()
{
yield gen.throttle(5);
console.log('at most 5');
yield setTimeout(gen.cb(), 1000);
console.log('continue in another generator');
yield* other_gen(); // same as 'yield gen.run(other_gen, gen.cb())'
}
function* other_gen()
{
yield setTimeout(gen.cb(), 1000);
console.log('finished in another generator');
}
function* test_throw()
{
var cb = gen.errorfirst();
try
{
yield setTimeout(function() { cb(new Error()); }, 500);
}
catch (e)
{
console.log('Catched '+e.stack);
}
console.log(yield setTimeout(gen.cb(), 500));
console.log('sleep');
console.log(yield setTimeout(gen.cb(), 500));
console.log('continue');
}
gen.run(test, function(result) { console.log(result); });
for (var i = 0; i < 15; i++) gen.run(test_throttle);
gen.run(test_throw);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_vitalif/gen-thread.git
git@gitee.com:mirrors_vitalif/gen-thread.git
mirrors_vitalif
gen-thread
gen-thread
master

搜索帮助