admin管理员组

文章数量:1334953

Why is this line a valid promise:

const promise = Promise.resolve('Hello');

But not this:

const otherPromise = () => {
  return Promise.resolve('Hello');
}

When trying to call the second example with:

function runOtherPromise() {
  otherPromise
    .then(v => console.log(v));
}

...I get TypeError: otherPromise.then is not a function. It works fine with the first example, though. I don't understand why the second example doesn't return a promise.

Why is this line a valid promise:

const promise = Promise.resolve('Hello');

But not this:

const otherPromise = () => {
  return Promise.resolve('Hello');
}

When trying to call the second example with:

function runOtherPromise() {
  otherPromise
    .then(v => console.log(v));
}

...I get TypeError: otherPromise.then is not a function. It works fine with the first example, though. I don't understand why the second example doesn't return a promise.

Share Improve this question asked Nov 13, 2017 at 12:22 ilovebigmacsilovebigmacs 9931 gold badge16 silver badges28 bronze badges 1
  • 4 otherPromise().then(v => console.log(v)); will do the job! – Dhaval Marthak Commented Nov 13, 2017 at 12:23
Add a ment  | 

1 Answer 1

Reset to default 6

otherPromise is a function, you should call it like below:

runOtherPromise() {
    otherPromise()
        .then(v => console.log(v));
}

本文标签: javascriptthen is not a functionStack Overflow