-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
42 lines (33 loc) · 984 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/node
(function () {
'use strict';
// remove the V8 Promise defer method()
delete Promise.defer;
// execute the deferred snippet (does nothing in V8)
require('./defer');
// constructor for a person object
function Person () {
// create a new deferred value
var _name = Promise.defer();
return {
// returns the promise of the deferred value
getName: function () {
return _name.promise;
},
// resolves the promise, when the _name gets set
setName: function (name) {
_name.resolve(name);
}
};
}
// create a Person instance
var person = Person();
// set the resolve handler
person.getName().then(function (name) {
console.log(name);
});
// set the name of the person asynchronously
setTimeout(function () {
person.setName('mightyplow');
}, 2000);
}());