Uncaught TypeError: XXX.then is not a function

Uncaught TypeError: XXX.then is not a function

我在遵循 promise 示例时遇到此错误。

let p2 = () => { return Promise.resolve("foo"); };

const test1 = () => {
   console.log("Test1 started");
   p2.then((res) => console.log("P2: " + res));
};

test1();

错误是:

Uncaught TypeError: p2.then is not a function

p2是一个函数,returns是一个promise,p2()是promise,所以你需要做

p2().then((res) => console.log("P2: " + res));