Question on writing functions that return a promise
Is it a bad idea to have code before a promise is returned? In my current setup, I have methods that reference the enclosing object using this, which is not accessible within the executor function of the promise. Will writing something like the following cause problems in my program:
const http = require('http');
class Thing {
fn() {
let options = { hostname: this.hostname }; // <---
return new Promise(function(resolve, reject) {
http.request(options, function(res) {
// grab data
res.on('end', function () {resolve(data);});
});
});
}
}
Posted Feb 16, 2017, 10:27 PM UTC
06