这是示例存储库。https://github.com/JiaLiPassion/zone-node https://github.com/JiaLiPassion/zone-node
首先,你需要使用最新版本的zone.js
要在nodejs中使用zone.js,您应该需要zone-node.js,
以下是一个运行示例。
require('./zone-node.js');
function log(str) {
Zone.root.run(function() {
console.log(str);
});
}
function foo() {
Zone.current.fork({
name: 'fooZone',
onScheduleTask: function(delegate, curr, target, task) {
log('Zone begin to schedule task not async yet ' + task.source);
return delegate.scheduleTask(target, task);
},
onInvokeTask: function(delegate, curr, target, task, applyThis, applyArgs) {
log('~~~~Zone before invoke async callback~~~~' + task.source);
delegate.invokeTask(target, task, applyThis, applyArgs);
log('~~~~Zone after invoke async callback~~~~' + task.source);
},
}).run(function() {
log('current zone, ' + Zone.current.name);
setTimeout(function() {
log('timeout is up, ', Zone.current.name);
}, 100);
});
};
foo();
在nodejs中运行后,输出将是。
current zone, fooZone
Zone begin to schedule task not async yetsetTimeout
~~~~Zone before invoke async callback~~~~setTimeout
timeout is up,
~~~~Zone after invoke async callback~~~~setTimeout