[英]Promise not resolved
I need the result of this axios call.我需要这个 axios 调用的结果。 I have tried many ways but I can´t get the promise resolved.
我尝试了很多方法,但我无法解决 promise。 I am allways getting promise pending with status resolved and [[PromiseValues]] This is the last one I have tried:
我总是让 promise 等待状态已解决,并且 [[PromiseValues]] 这是我尝试过的最后一个:
function getResult() {
let promise = new Promise(function(resolve, reject) {
resolve(axios.get(URL))
});
return promise;
};
let theResult = getResult().then(function(result) {
return result.data
});
axios.get
- a method that returns a Promise. axios.get
- 一种返回 Promise 的方法。
Either任何一个
axios.get(URL).then(result => result.data);
or或者
function getResult(url) {
return axios.get(url);
};
let theResult = getResult('<URL goes here>').then(function(result) {
return result.data
});
You cannot expect return value from promise by doing var data = getResult(url);
您不能通过执行
var data = getResult(url);
. . data will have promise object which has its status and value(if resolved or rejected).
数据将有promise object有其状态和价值(如果解决或拒绝)。
function getResult() {
let promise = new Promise(function (resolve, reject) {
resolve(axios.get("https://api.github.com/users/mralexgray/repos"))
});
return promise;
};
getResult().then((result) => {
let theResult = result.data;
/** process result **/
}).catch((err) => {
console.log(err, "error");
})
You are resolving your promise
to another promise axios.get(URL)
, because of that you are getting pending
as a status even after calling resolve
.您正在将您的
promise
解析为另一个 promise axios.get(URL)
,因为即使在调用resolve
之后,您仍处于pending
状态。 Try to resolve it with any other value(non-promise) you will get resolved status.尝试使用任何其他值(非承诺)解决它,您将获得已解决状态。
I would recommend you to go through article.我会通过文章向您推荐 go。
It should work if you do like this:如果您这样做,它应该可以工作:
axios.get(URL).then(res=>resolve(res.data)) axios.get(URL).then(res=>resolve(res.data))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.