1 |
|
2 | /**
|
3 | * Example demonstrates that Fibers does not block whole process while yielding
|
4 | */
|
5 |
|
6 | var Sync = require('sync');
|
7 |
|
8 | // Some asynchronous function
|
9 | function someAsyncFunction(a, b, callback) {
|
10 | setTimeout(function(){
|
11 | callback(null, a + b);
|
12 | }, 1000)
|
13 | }
|
14 |
|
15 | // Simply print message after 500ms in main loop
|
16 | setTimeout(function(){
|
17 | console.log('500 ms')
|
18 | }, 500)
|
19 |
|
20 | // Here we need to start new Fiber inside of which we can do our tests
|
21 | Sync(function(){
|
22 |
|
23 | // Call the function synchronously
|
24 | // current fiber will yield for 1 sec, so it should be returned later than Timeout above
|
25 | var result = someAsyncFunction.sync(null, 2, 3);
|
26 | console.log(result); // will print 5
|
27 | }) |
\ | No newline at end of file |