[英]Using async-await not returning the values
我使用以下有效的代码,我能够获取传播中的数据(然后),http200
Promise.all([
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'run:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}),
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'exe:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
})
]).then(axios.spread((...responses: AxiosResponse[]) => {
const aToken = responses[0];
const bToken = responses[1];
}).catch(function(error: any) {
console.log("Error: " + error);
});
现在我想用异步 function包装它,它返回两个响应(responses[0],responses[1])
我已经创建了这个 function
const FetchData = async (): Promise<{ run: string; app: string }> => {
let aToken:string;
let bToken:string;
Promise.all([
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'run:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}),
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'exe:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
})
]).then(axios.spread((...responses: AxiosResponse[]) => {
aToken = responses[0];
bToken = responses[1];
}).catch(function(error: any) {
console.log("Error: " + error);
});
// add this return
return {
run: aToken ,
app: bToken
};
)}
现在我创建了 function 来调用它并获取令牌
async function main() {
let val = await FetchData();
console.log(val)
}
问题是我在val
属性中得到了空值,而在 main function 中放置了一个断点,我看到我进入了控制台,然后才真正从).then(axios.spread((
,知道如何采用代码从扩展运算符返回值,然后调用控制台?
const res = await Promist.all([...])
return { run: res[0], app: res[1] }
您没有正确使用async-await
。 您需要等待Promise.all
。 一旦两个承诺都得到解决,响应数据将位于您从axios
获得的每个响应 object 中名为data
的属性中。 如果你需要整个响应 object 那么你可以解构它
这是一个例子
const FetchData = async (): Promise<{ run: string; app: string }> => {
try {
const [response1, response2] = await Promise.all([
axios({
...
}),
axios({
...
})
]);
return {
run: response1,
app: response2
};
} catch (error) {
console.log(error);
}
};
这是一个从jsonplaceholder api的 2 个端点获取数据的演示
您需要更改您的退货声明的 position:
更新的代码有变化:
const FetchData = (): Promise<{ run: string; app: string }> => {
let aToken:string;
let bToken:string;
// Return the promise here
return Promise.all([
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'run:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}),
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({
'grant_type': 'client_credentials'
}, {
'scope': 'exe:crud'
}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
})
]).then(axios.spread((...responses: AxiosResponse[]) => {
aToken = responses[0];
bToken = responses[1];
// This return is for getting the data once the promise is resolved either with then or await
return {
run: aToken ,
app: bToken
};
}).catch(function(error: any) {
console.log("Error: " + error);
return error;
});
)}
由于您在 promise 被解析之前返回值,因此您不会获得任何数据。 您需要返回 promise 并在内部返回所需的数据,以便在调用 function 解决 promise 后获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.