Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Promise#finally proposal Stage 2 TC39 0716 #238

Closed
wants to merge 1 commit into from

Conversation

clavecoder
Copy link
Contributor

@clavecoder clavecoder commented Sep 5, 2016

Promise#finally proposal Stage 2 from TC39 meeting on 2016-07.

Used implementation of String#padStart, String#trimLeft, and Promise#then as examples.

Initial open items

  • Not able to use finally for the function name, perhaps because it is a reserved keyword. Temporarily used _finally for implementation and test (commented below).
  • Did not use polyfill's method of setting species. Coded to assume core-js Promise polyfill will set species, but, if I'm not mistaken, it's possible that finally may be required without the Promise polyfill. Since core-js has its own method of setting species, what should be done?
  • The failing test is precisely about Promise not having the right species (I think), but I'm not sure why, it shows up in the commonjs test for delay
    image
  • The finally implementation is not in an internal file, but directly in the imported one. I am not supposing that it would be used by other functions in the library. Should I rather assume that it may be?

var $export = require('./_export');

$export($export.P, 'Promise', {
'finally': function _finally(onFinally){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used _finally. Error generated without the underscore.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 'finally': function(onFinally). For ES6+ engines it will create the correct .name, for old engines we can't do anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 'finally': function(onFinally)

Done (not checked in yet...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked in.

var newPromise = Promise.prototype.then.call(
this, // throw if IsPromise(this) is not true
function(x) {
return new C()(function(resolve) {
Copy link

@Jessidhia Jessidhia Sep 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're calling the result of new C() as a function o_O

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It crashes at runtime 🙀

I've asked the author about it, but it's most likely accidental leftovers from refactoring.

I am using a previous version of the polyfill that had a much simpler speciesConstructor implementation (https://github.com/tc39/proposal-promise-finally/blob/c626fb5857e0564eff6ab6a5b5e2b9383c156ba7/polyfill.js).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kovensky:

  1. Thanks for the older implementation reference. That's what I would expect. I like to move methodically through uncharted waters.
  2. I'll use the core-js internal implementation of speciesConstructor used in es6.promise.js
  3. Thanks for the quick responses!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It behaves correctly without the excess ().

Copy link
Contributor Author

@clavecoder clavecoder Sep 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked in.
Summary of changes: #238 (comment)

@clavecoder
Copy link
Contributor Author

clavecoder commented Sep 5, 2016

Checked in changes per comments. However my added test file is failing hard. Changes made:

  1. es7/promise.js: Added require of es6.promise before es7.promise.finally
  2. fn/promise.js: Added require of es7.promise.finally after es6.promise
  3. Removed fn/promise/ in favor of fn/promise.js
  4. library/es7/promise.js: Added require of es6.promise before es7.promise.finally
  5. library/modules/es7.promise.finally.js:
    1. changed to implicit name for finally
    2. now uses speciesConstructor
    3. Removed polyfill regression of using Promise instance as function.
  6. modules/es7.promise.finally.js: ditto
  7. Added /tests/library/es7.promise.finally.ls
    1. Mirrored behavior of /tests/tests/es7.promise.finally.ls
  8. /tests/tests/es7.promise.finally.ls
    1. Removed extraneous comment
    2. Removed test for name of Promise#finally
    3. Added JavaScript pseudocode for Promise#finally implementation tests.

Open issues:

  • tests/library.js is failing because Promise#finally is not found for test file /tests/library/es7.promise.finally.ls. I've tried tracing the require path and, of course, it seems right, but there's obviously something wrong. Also remember that the implementation was changed to use an anonymous function for the property assignment. I don't know if it is related. Error message:
    image
    The source code for the error line is /tests/library/es7.promise.finally.ls/L7
  assert.isFunction Promise::finally
  • I'm finding LiveScript a bit beyond me for creating the unit tests without an example. I've added JavaScript pseudocode in comments for /tests/library/es7.promise.finally.ls and /tests/tests/es7.promise.finally.ls. If you can point me to some LiveScript tests that are doing something similar, I should be able to tease out the implementation.

Here's the pseudocode:

  #
  # when value is returned, test finally is called, then onFulfilled is reached, not onRejected
  # js:
  # var finallyReached1 = false;
  # var expectedValue1 = 1;
  # var p1 = new Promise((resolve, reject) => resolve(expectedValue1));
  # p1 = p1.finally(() => finallyReached1 = true;);
  # p1.then(
  #   val => {
  #     assert.equal(finallyReached1, true);
  #     assert.equal(value, expectedValue1);
  #   },
  #   reason => { assert.fail('onRejected should not be reached') });
  #
  # when exception is thrown, test finally is called, then onRejected is reached, not onFulfilled
  # js:
  # var finallyReached2 = false;
  # var expectedReason1 = 1;
  # var p2 = new Promise((resolve, reject) => reject(expectedReason1));
  # p2 = p2.finally(() => finallyReached2 = true;);
  # p2.then(
  #   val => { assert.fail('onFulfilled should not be reached') },
  #   reason => {
  #     assert.equal(finallyReached2, true);
  #     assert.equal(reason, expectedReason1);
  #   });
  #
  # when onFinally is not a function nothing untoward happens
  # var expectedValue2 = 1;
  # var p1 = new Promise((resolve, reject) => resolve(expectedValue2));
  # p1 = p1.finally(3);
  # p1.then(
  #   val => {
  #     assert.equal(value, expectedValue2);
  #   },
  #   reason => { assert.fail('onRejected should not be reached') });
  # var expectedReason2 = 1;
  # var p2 = new Promise((resolve, reject) => reject(expectedReason2));
  # p2 = p2.finally(3);
  # p2.then(
  #   val => { assert.fail('onFulfilled should not be reached') },
  #   reason => {
  #     assert.equal(reason, expectedReason2);
  #   });
  #

@clavecoder
Copy link
Contributor Author

Any test code you can point me to that work similarly to the pseudocode? I should have more time this weekend.

@cvbuelow
Copy link

Is this still being worked on?

@zloirock
Copy link
Owner

ed59a9c, 86ed553, fd70e24, 0bc7a9e

@zloirock zloirock closed this Jul 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants