I faced with situation where I need to pass some values to Promise handler. Below is example of situation
function promiseFunct(x){
return new Promise(function(resolve, reject){
if(x>0) resolve(x);
else if(x==0) resolve(1);
else resolve(0);
});
}
promiseFunct(-100).then(function(response){
var someObj = { len: 124 };
return promiseFunct(response).bind(someObj);
}).then(function(response){
if(this.len) console.log(this.len); //not getting access here, this is Window
});
I'm trying to bind someObj and then get access to it in handler, but no success. Is there some elegant solution to pass some object to promise handler, except passing to Promise and then resolving inside Promise?
Found simple solution, but there is no guarantee, that it's most elegant.
promiseFunct(-100).bind({}).then(function(response){
this.len = 124 ;
return promiseFunct(response);
}).then(function(response){
if(this.len) console.log(this.len); // showing 124
});
With bind({})
setting own Promise chain context which will not cross with Window
, for example. If I had len
value outside I could use bind({len: len})
Then I can use this.propertyName
to get or set desired property for using in next handlers.