UNPKG

872 BJavaScriptView Raw
1
2/**
3 * The example of some object which has a property and
4 * asynchronous method which uses this.someProperty to get it's value
5 * and we want to call this method synchronously
6 */
7
8var Sync = require('sync');
9
10// the object
11var someObject = {
12
13 someProperty : 2,
14
15 someAsyncMethod : function someAsyncMethod(b, callback) {
16 var self = this;
17 setTimeout(function(){
18 callback(null, self.someProperty + b);
19 }, 1000)
20 }
21}
22
23// Here we need to start new Fiber inside of which we can do our tests
24Sync(function(){
25
26 // Here we need to set 'this' context for someAsyncMethod
27 // It will add passed argument to someObject.someProperty and return the result
28 // It's works the same way as Function.prototyle.call
29 var result = someObject.someAsyncMethod.sync(someObject, 3);
30 console.log(result); // 5
31
32})
\No newline at end of file