I have a weird scenario that I don't what caused this. I have a function that returns a promise and chain to it a 'then' method. Inside the then method, I can access one of the outer variables, but the other one is undefined.
I thought that I might have a with that name used in the 'then' function and due to hoisting the value is undefined, but there is not.
Here is a simplified version of my case, wondering what can cause such a scenario:
class MessageSender {
constructor(){
}
sendMessage(message, options) {
return somethingThatReturnPromise()
.then(function (response) {
// parameter 'message' is object as it should be,
// but'options' is undefined.
return response;
})
return promise;
}
}
Thanks
It seems to work just fine.
function sendMessage(message, options) {
return somethingThatReturnPromise()
.then(function (response) {
console.log('message: ' + message);
console.log('options: ' + options);
return response;
});
}
function somethingThatReturnPromise(){
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve();
}, 100);
});
}
console.log('Starting...');
sendMessage('THE MESSAGE', 'THE OPTIONS').then(function(){
console.log('All done.');
});
Returns
me@pc:~/dev/test$ node asdf.js
Starting...
message: THE MESSAGE
options: THE OPTIONS
All done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.