According to the API reference Q provides promise extensions for working with objects, but they don't behave as I would expect.
Consider the following line of code:
Q({foo:"bar"}).set("foo","baz").then(console.log);
I would have expected {"foo": "baz"} to be printed, but it's actually undefined.
Do I misuderstand the set method, or am I just using it in a wrong way?
The
Q.setis used to augment an object, in the future and it doesn't return the augmented object (in fact, it doesn't return anything at all). That is why you are gettingundefinedin theconsole.log.You need to use it like this
This would print
The property
foois set asbazin theobjand since it is passed along the promise chain, we need to explicitly print it.You can use the sister function,
Q.get, like thisYou might be wondering why you cannot do
It will not work, because of the same reason why
setdidn't work in the first place.setdoesn't resolve with the modified object. That's why we need to explicitly get it from the actual object.FWIW, I found this,
setnot being chainable, bit odd. So, I submitted a pull request to the Q library to change this, and it got rejected.