promise.then() 在我向其中发送另一个承诺时不起作用
promise.then() not working when I send in another promise into it
我有 2 个函数 getAccountInfo() 和 getAdjustmentsInfo(accountInfo) 它们都是 return 一个新的承诺。唯一不同的是第二个函数需要从第一个函数return编辑的信息。
我试着先声明这两个函数,然后使用 then() 一个一个地调用它们。但它并没有像我预期的那样工作。
这些是函数,您可以看到第二个函数需要 account_code 来自第一个 accountInfo 对象:
function getAccountInfo() {
return new Promise((resolve, reject) => {
getAccountCallbackFunc((errResponse, response) => {
if (errResponse) {
return reject(errResponse);
}
resolve(response);
});
});
}
function getAdjustmentsInfo(accountInfo) {
return new Promise((resolve, reject) => {
getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
if (errResponse) {
reject(errResponse);
}
if (response) {
resolve(response);
}
});
});
}
这是调用函数的控制器代码:
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
})
.then(getAdjustmentsInfo(accountInfo))
.catch(err => console.log(err));
所以我先运行 getAccountInfo()函数然后运行第一个then()将账户信息保存到外部变量accountInfo。
接下来我 运行 第二个 then() 试图将 accountInfo 传递给第二个函数,但它不起作用,第二个函数从未被调用过。那是我想念的东西吗?请告诉我。
您正在立即评估 getAdjustmentsInfo
方法,因为它不在回调中。尝试:
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
})
.then(() => getAdjustmentsInfo(accountInfo))
.catch(err => console.log(err));
甚至更好:
var accountInfo = getAccountInfo()
.then(response => {
console.log(response.data.accounts.account);
return response.data.accounts.account;
})
.then(account => getAdjustmentsInfo(account))
.catch(err => {
console.log(err));
return {};
})
您直接呼叫 getAdjustmentsInfo
而不是等待 getAccountInfo
,并且您没有 return 在第一个 then
中呼叫 Promise
。我想这就是你的意思:
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
return getAdjustmentsInfo(accountInfo)
})
.catch(err => console.log(err));
您的代码中有几个错误。首先是你没有返回任何东西,也没有解决 accountInfo = response.data.accounts.account;console.log(accountInfo);
行中的任何承诺,所以 .then(getAdjustmentsInfo(accountInfo))
不会被调用。其次,我想 .then()
的第一个参数总是一个回调,第一个参数是从前一个 Promise 返回的东西。
你的函数getAccountInfo()
returns一个承诺。当这个承诺得到解决时,你可以
直接使用如下。
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
getAdjustmentsInfo(accountInfo)
})
.then(resultFromPreviousPromise => {
//Use the resultFromPreviousPromise if wanted else you can skip this .then()
})
.catch(err => console.log(err));
尝试一下,希望对您有所帮助。
function getAccountInfo() {
return new Promise((resolve, reject) => {
getAccountCallbackFunc((errResponse, response) => {
if (errResponse) {
return reject(errResponse);
}
resolve(response);
});
});
}
function getAdjustmentsInfo(accountInfo) {
getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
if (errResponse) {
return console.log(errResponse);
}
resolve(response);
});
}
var promise = new Promise(function(resolve, reject){
getAccountInfo()
.then(response => {
resolve(response.data.accounts.account);
console.log(accountInfo);
})
}).then(function(data){
getAdjustmentsInfo(data);
}).catch(function(err){console.log(err)});
我有 2 个函数 getAccountInfo() 和 getAdjustmentsInfo(accountInfo) 它们都是 return 一个新的承诺。唯一不同的是第二个函数需要从第一个函数return编辑的信息。
我试着先声明这两个函数,然后使用 then() 一个一个地调用它们。但它并没有像我预期的那样工作。
这些是函数,您可以看到第二个函数需要 account_code 来自第一个 accountInfo 对象:
function getAccountInfo() {
return new Promise((resolve, reject) => {
getAccountCallbackFunc((errResponse, response) => {
if (errResponse) {
return reject(errResponse);
}
resolve(response);
});
});
}
function getAdjustmentsInfo(accountInfo) {
return new Promise((resolve, reject) => {
getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
if (errResponse) {
reject(errResponse);
}
if (response) {
resolve(response);
}
});
});
}
这是调用函数的控制器代码:
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
})
.then(getAdjustmentsInfo(accountInfo))
.catch(err => console.log(err));
所以我先运行 getAccountInfo()函数然后运行第一个then()将账户信息保存到外部变量accountInfo。 接下来我 运行 第二个 then() 试图将 accountInfo 传递给第二个函数,但它不起作用,第二个函数从未被调用过。那是我想念的东西吗?请告诉我。
您正在立即评估 getAdjustmentsInfo
方法,因为它不在回调中。尝试:
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
})
.then(() => getAdjustmentsInfo(accountInfo))
.catch(err => console.log(err));
甚至更好:
var accountInfo = getAccountInfo()
.then(response => {
console.log(response.data.accounts.account);
return response.data.accounts.account;
})
.then(account => getAdjustmentsInfo(account))
.catch(err => {
console.log(err));
return {};
})
您直接呼叫 getAdjustmentsInfo
而不是等待 getAccountInfo
,并且您没有 return 在第一个 then
中呼叫 Promise
。我想这就是你的意思:
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
return getAdjustmentsInfo(accountInfo)
})
.catch(err => console.log(err));
您的代码中有几个错误。首先是你没有返回任何东西,也没有解决 accountInfo = response.data.accounts.account;console.log(accountInfo);
行中的任何承诺,所以 .then(getAdjustmentsInfo(accountInfo))
不会被调用。其次,我想 .then()
的第一个参数总是一个回调,第一个参数是从前一个 Promise 返回的东西。
你的函数getAccountInfo()
returns一个承诺。当这个承诺得到解决时,你可以
直接使用如下。
var accountInfo = {};
getAccountInfo()
.then(response => {
accountInfo = response.data.accounts.account;
console.log(accountInfo);
getAdjustmentsInfo(accountInfo)
})
.then(resultFromPreviousPromise => {
//Use the resultFromPreviousPromise if wanted else you can skip this .then()
})
.catch(err => console.log(err));
尝试一下,希望对您有所帮助。
function getAccountInfo() {
return new Promise((resolve, reject) => {
getAccountCallbackFunc((errResponse, response) => {
if (errResponse) {
return reject(errResponse);
}
resolve(response);
});
});
}
function getAdjustmentsInfo(accountInfo) {
getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
if (errResponse) {
return console.log(errResponse);
}
resolve(response);
});
}
var promise = new Promise(function(resolve, reject){
getAccountInfo()
.then(response => {
resolve(response.data.accounts.account);
console.log(accountInfo);
})
}).then(function(data){
getAdjustmentsInfo(data);
}).catch(function(err){console.log(err)});