gpt4 book ai didi

javascript - 未捕获的类型错误 : undefined is not a function

转载 作者:行者123 更新时间:2023-11-28 01:02:15 24 4
gpt4 key购买 nike

下面的 JavaScript 代码片段在 .done 语句上出现错误。错误是“未捕获的类型错误:未定义不是函数”既然我们可以使用 did 和 Promise 知道为什么会出现这个错误吗?

var promise = new Promise(function(resolve,reject)
{

if(true)
{
console.log("1");

}
else
console.log("2");


});
promise.then( function(data1) {

if(true)
{
console.log("3");
}

else
console.log("4");
})
.then( function(data2) {

if(true)
{
console.log("5");
}
else
console.log("6");

})
.done(
function(response) {
console.log("done")
});
.fail(
function() {
console.log("fail");
});

最佳答案

Promise API 中没有 donefail 这样的方法,只有 thencatch 在对象级别:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Methods

Promise.prototype.then(onFulfilled, onRejected)

Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.

Promise.prototype.catch(onRejected)

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it iscalled, or to its original fulfillment value if the promise is insteadfulfilled.

关于javascript - 未捕获的类型错误 : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25483899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com