Is ".then(function(a){ return a; })" a no-op for promises?
It seems to me like
.then(function (a) { return a; })
is just a no-op. Right?
Yes.1
It is useless and should be omitted.
What's going on and why did the author write it that way?
It's a blunder. Or the author didn't understand promises.
1: If they aren't the same, what's the difference?
As always, there are some edge cases. Really weird ones. That no-one should use (without extensive commenting):
a) it returns a new promise instance, a distinct object, to avoid sharing. However, .then()
would as well.
b) a
is tested again for its thenable-ness. If it suddenly became a promise since the fulfillment, it now will be awaited. This would be awful of course.
Bergi's answer is right, but just to demonstrate a case where it's not a no-op here is a contrived example where it's not a no-op:
o = {};
Promise.resolve(o).then(o => o.then = () => {}); // make thenable
Promise.resolve(o).then(console.log); // logs the object
Promise.resolve(o).then(x => x).then(console.log); // doesn't log the object
In general, don't do then(function(a) { return a; })