Suppose person A tells person B, "If you perform (service X) for me, I will donate $1,000 to Save the Whales (but no other compensation)." Neither A nor B has any connection to whales and will not benefit from this donation, but B happens to like whales and agrees anyway. Does this still count as consideration, even though there was never any possibility of B getting anything out of it? (United States)
Does this still count as consideration, even though there was never any possibility of B getting anything out of it?
Yes. The sole fact that the promisee and the beneficiary are different and unrelated entities does not invalidate a contract.
In terms of the Restatement (Second) of Contracts at § 302(1)(b), what matters is that "the circumstances indicate that the promisee intends to give the beneficiary the benefit of the promised performance".
Consideration must pass from the promisee, it doesn’t have to pass to the promisor.
However, at common law, if A doesn’t perform, B can sue but Save the Wales can’t. This is part of the doctrine of privity. The landmark cases are Beswick v Beswick  UKHL 2,  AC 58 in the UK and Coulls v. Bagot’s Executor and Trustee Co Ltd (1967) 119 CLR 460 in Australia. In both cases, a third party beneficiary was unable to obtain performance on their own behalf, however, also in both cases, they were both executors of the estate of a party to the contract and could sue in that capacity.
This is still good law in Australia (except Queensland, Northern Territory and Western Australia) but the Contracts (Rights of Third Parties) Act 1999 has rendered it obsolete in the UK. The problem can be overcome by making the third party a party to the contract for a peppercorn consideration, say $1.