Skip to content Skip to sidebar Skip to footer

Why Can't I Use Array.prototype.join.call As A Callback Of A Promise?

I'm trying to run this code: Which is (according to me) an equivalent of this: It actually fails with error: Uncaught (in promise) TypeError: undefined is not a function Can so

Solution 1:

The Function.prototype.call() method is not a function. It needs a this reference.

"The call() method calls a function with a given this value and arguments provided individually."

You will need to pass the response directly to it, or just call:

response.join()

Working example

Promise.resolve([1, 2])
  .then(response =>Array.prototype.join.call(response))
  .then(console.log)

Alternatively...

You can create a binder function.

constbindMethod = (method, scope) => ((ref) =>(value) =>
      ref.call.bind(ref)(value))(scope === undefined
        ? method
        : scope.prototype[method])

Promise.resolve([1, 2])
  .then(bindMethod(Array.prototype.join))
  .then(console.log)
  
Promise.resolve([1, 2])
  .then(bindMethod('join', Array))
  .then(console.log)

Post a Comment for "Why Can't I Use Array.prototype.join.call As A Callback Of A Promise?"