.then不是一个功能

ilo*_*acs 1 javascript es6-promise

为什么这行是有效的承诺:

const promise = Promise.resolve('Hello');
Run Code Online (Sandbox Code Playgroud)

但这不是:

const otherPromise = () => {
  return Promise.resolve('Hello');
}
Run Code Online (Sandbox Code Playgroud)

尝试使用以下示例调用第二个示例时:

function runOtherPromise() {
  otherPromise
    .then(v => console.log(v));
}
Run Code Online (Sandbox Code Playgroud)

...我明白了TypeError: otherPromise.then is not a function。不过,在第一个示例中它可以正常工作。我不明白为什么第二个示例不返回承诺。

Fal*_*aly 6

otherPromise是一个函数,应按以下方式调用它:

runOtherPromise() {
    otherPromise()
        .then(v => console.log(v));
}
Run Code Online (Sandbox Code Playgroud)