Closed Bug 1039153 Opened 12 years ago Closed 12 years ago

Success handler for promises from "core/promise" fails unexpectedly

Categories

(Add-on SDK Graveyard :: General, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INVALID

People

(Reporter: xrsquared, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0 (Beta/Release) Build ID: 20140605174243 Steps to reproduce: Certain success handler for promise.then() will cause its returned promise to fail. Run the following: let arr = []; let d = defer(); d.resolve(true); d.promise. then(arr.push). then(r => console.log("Success, result is " + r), console.log); Actual results: a TypeError and a nasty trace is logged. (from the error handler of the second then()) Expected results: instead of logging "Success, result is true"
Corrections, the expected result should be "Success, result is 1" (return value of arr.push) here is a version that will work expectedly let arr = []; let d = defer(); d.resolve(true); d.promise. then(r => arr.push(r)). then(r => console.log("Success, result is " + r), console.log);
It's actually correct that is failing.: you have an exception on the first `then`, that is properly catch in the following one. With `then(arr.push)` you're basically passing only the reference to `push`, so the `this` object internally won't point to `arr`. It's like doing: let arr = []; let f = arr.push; f(12); // exception You have to bind `push` with `arr`: … then(arr.push.bind(arr)). … Or, as you did already in your second comment, call it as method of `arr`.
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → INVALID
(In reply to Matteo Ferretti [:matteo] [:zer0] from comment #2) > It's actually correct that is failing.: you have an exception on the first > `then`, that is properly catch in the following one. > > With `then(arr.push)` you're basically passing only the reference to `push`, > so the `this` object internally won't point to `arr`. It's like doing: > > let arr = []; > let f = arr.push; > f(12); // exception > > You have to bind `push` with `arr`: > > … > then(arr.push.bind(arr)). > … > > Or, as you did already in your second comment, call it as method of `arr`. Thanks a lot for the insight! I'm still pretty new to JS and this taught me a lot! I would of never thought it is related to prototypical inheritance.
You need to log in before you can comment on or make changes to this bug.