javascript异步:无法读取属性';然后';未定义的
我试图一个接一个地触发3个函数。每次完成后,下一步将启动我的代码:-javascript异步:无法读取属性';然后';未定义的,javascript,asynchronous,Javascript,Asynchronous,我试图一个接一个地触发3个函数。每次完成后,下一步将启动我的代码:- getInfo(f1 , f2).then(function() { setInfo().then(function(callback) { Three(); }); }); 我得到这个错误:- >TypeError: Cannot read property 'then' of undefined 更新 function setInfo(){ alert('done')
getInfo(f1 , f2).then(function() {
setInfo().then(function(callback) {
Three();
});
});
我得到这个错误:-
>TypeError: Cannot read property 'then' of undefined
更新
function setInfo(){
alert('done')
}
你需要真正的锁链 让我们假设您的代码有一个类似于下面的API
getInfo
,setInfo
,可能还有三个
都是异步的。它们返回解析为某个值的Promise
s
function getInfo(a, b) {
return Promise.resolve(true);
}
function setInfo() {
alert("done");
return Promise.resolve(true);
}
function Three() {
return Promise.resolve(true);
}
您需要将它们实际链接在一起,以便它们都互相等待并向上传播值
const f1 = "foo";
const f2 = "bar";
getInfo(f1, f2)
.then(() => setInfo())
.then(cb => Three());
或者getInfo或setInfo(或两者!)未在您试图访问它的范围内定义。尝试向我们分享更多的代码,看看问题出在哪里。需要了解更多关于
setInfo()
的信息getInfo()
返回什么?setInfo()
返回什么?为什么不从中返回,然后从回调继续链?函数getInfo
和setInfo
必须返回一个Promise
您实际想做什么?正在阻止,并且不返回承诺。不能对没有then()
方法的对象调用then()
。