babel / kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
ISC License
514 stars 41 forks source link

Optimize Promise.resolve().then pattern. #17

Closed KamilaBorowska closed 8 years ago

KamilaBorowska commented 8 years ago

README shows the following code.

function test() {
  return Promise.resolve().then(function () {
    return db.destroy();
  }).then(function () {});
}

I believe this can be optimized into the following (Promise.resolve causes promise to stay as promise, while changing non-promise values into promises).

function test() {
  return Promise.resolve(db.destroy()).then(function () {});
}

This isn't particularly important, because it's purely an optimization, just bringing it up for later.

marten-de-vries commented 8 years ago

Thanks for your bug report!

I'm afraid your optimalization wouldn't work: what if db.destroy() throws some error? In the first case, it would be transferred to the caller via the promise chain (you could add a .catch() block), but in the second case, it would throw an exception.

Now last time I checked this is actually something that regenerator does, but as far as I understand the spec, the current behaviour is actually correct.

Feel free to re-open if you think I missed something. :)

KamilaBorowska commented 8 years ago

Oh, fair enough. I didn't even realize this edge case.

marten-de-vries commented 8 years ago

One of the advantages of writing such a project is learning a lot about edge cases - and I'm sure there are still plenty out there, so keep the bugs reports coming if you spot something else.