| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -859,6 +859,34 @@ will throw an error. If `original` is a function but its last argument is not | |||
| 859 | 859 | an error-first callback, it will still be passed an error-first | |
| 860 | 860 | callback as its last argument. | |
| 861 | 861 | ||
| 862 | + Using `promisify()` on class methods or other methods that use `this` may not | ||
| 863 | + work as expected unless handled specially: | ||
| 864 | + | ||
| 865 | + ```js | ||
| 866 | + const util = require('util'); | ||
| 867 | + | ||
| 868 | + class Foo { | ||
| 869 | + constructor() { | ||
| 870 | + this.a = 42; | ||
| 871 | + } | ||
| 872 | + | ||
| 873 | + bar(callback) { | ||
| 874 | + callback(null, this.a); | ||
| 875 | + } | ||
| 876 | + } | ||
| 877 | + | ||
| 878 | + const foo = new Foo(); | ||
| 879 | + | ||
| 880 | + const naiveBar = util.promisify(foo.bar); | ||
| 881 | + // TypeError: Cannot read property 'a' of undefined | ||
| 882 | + // naiveBar().then(a => console.log(a)); | ||
| 883 | + | ||
| 884 | + naiveBar.call(foo).then((a) => console.log(a)); // '42' | ||
| 885 | + | ||
| 886 | + const bindBar = naiveBar.bind(foo); | ||
| 887 | + bindBar().then((a) => console.log(a)); // '42' | ||
| 888 | + ``` | ||
| 889 | + | ||
| 862 | 890 | ### Custom promisified functions | |
| 863 | 891 | ||
| 864 | 892 | Using the `util.promisify.custom` symbol one can override the return value of | |
| Back | FazBrowse Home | New Git URL |
0 commit comments