1

Basically, I have a function that returns a promise and calling finally() doesn't appear to be working on Edge. It is fine in IE11 (weird huh?) The rest of the Promise API works great. I read this could be a bug with either Edge or the Babel Polyfill and a workaround is to wrap it somehow, but I have not seen a code example.

API

I have an API that returns a promise

/**
 * Return a promise containing a single employee.
 *
 * @param {string} [employeeID] The employeeID.
 *
 * @return {promise} The promise. 
 */
export function getEmployee(employeeID) {
    return fetch(api + "?employeeID=" + employeeID)
        .then((resp) => {
            return resp.json();
        });
}

Calling Code

    getEmployee(this.props.id)
        .then((json) => {
            this.setState({
               // do some stuff
            });
        })
        .finally(() => { });

Notes

  • Using Babel Polyfill
  • Using WhatWG Fetch
  • Using Webpack

Error

SCRIPT438: Object doesn't support property or method 'finally'

AmirBll
  • 1,081
  • 1
  • 13
  • 25
Eric Harms
  • 827
  • 1
  • 7
  • 13
  • 2
    Doing this in Edge -> `Promise.resolve().then(() => console.log("do this")).finally(() => console.log("finally"))` Shows `do this` `finally`, Are you able to knock up a reproducible test case. – Keith May 08 '19 at 21:43
  • It may have to do with `fetch` not returning a *native, built-in* Promise (that would inherit from `Promise.prototype.finally`), but that it returns a polyfill version that does not have a `.finally` method (not that sure though) – CertainPerformance May 08 '19 at 21:46
  • While I've seen weird bugs in Edge, I am not convinced about this one yet. What do you mean by not appearing to be working? Does it throw an error? Or does it jjust hang? Lets try to replucate the problem, show us some code that you consider not working in Edge, and we can run it ourselves. – Tamas Hegedus May 08 '19 at 21:47
  • Let me add the error above. – Eric Harms May 10 '19 at 13:38
  • Could it be an issue with Whatwg-fetch polyfill? – Eric Harms May 10 '19 at 13:38
  • `.finally(() => { });` I dont think its valid ES6 – Kunal Mukherjee May 10 '19 at 13:39
  • () => {} is function shorthand and transpiled via babel. The error posted is about the method finally(). – Eric Harms May 10 '19 at 13:58
  • @EricHarms the reason the polyfill works in IE11 but not Edge is because the shim does not polyfill each method individually. Since IE11 has _no_ `Promise` support, the entire polyfill is dropped in, but since Edge has ES2015 `Promise` support (`.finally()` was added in ES2017, but never implemented by Edge), the shim does not properly detect that `.finally()` needs to be patched on the existing native `Promise` implementation in Edge. My answer in the duplicate provides a simple patch that falls back to a polyfill if `.finally` does not already exist. – Patrick Roberts May 10 '19 at 15:25
  • Thanks for everything. Looking at the duplicate now. Much appreciated – Eric Harms May 10 '19 at 19:13
  • I dropped the following code in before my main entry point and after the babel polyfill. No luck. Promise.prototype.finally = Promise.prototype.finally || { finally (fn) { const onFinally = cb => Promise.resolve(fn()).then(cb); return this.then( result => onFinally(() => result), reason => onFinally(() => Promise.reject(reason)) ); } }.finally; – Eric Harms May 10 '19 at 19:21

0 Answers0