Javascript async()等待返回承诺而不是数据
我不明白为什么这段代码返回承诺数组,而最后一位返回对象的实际数据数组: 异步=>{ const[user,posts]=wait Promise.all[ 取回https://jsonplaceholder.typicode.com/users', 取回https://jsonplaceholder.typicode.com/posts' ]。然后[res,res2]=>[res.json,res2.json] 。然后[d1,d2]=>{ 控制台。logd1、d2; }; }; //承诺{[[PromiseStatus]]:已解决,[[PromiseValue]]:数组10} //承诺{[[PromiseStatus]]:已解决,[[PromiseValue]]:Array100}res.json返回另一个承诺,因此您必须在承诺链中返回该承诺,或者在该承诺上使用wait or。因为您使用[res.json,res2.json]作为承诺链中的返回值,所以您将承诺隐藏在该数组中,因此承诺链根本不会等待它们。承诺本身就是回报,因此承诺。所有人都不知道他们在那里,也不等待他们 我建议将每个res.json链接到它自己的父级:Javascript async()等待返回承诺而不是数据,javascript,node.js,async-await,ecmascript-2017,Javascript,Node.js,Async Await,Ecmascript 2017,我不明白为什么这段代码返回承诺数组,而最后一位返回对象的实际数据数组: 异步=>{ const[user,posts]=wait Promise.all[ 取回https://jsonplaceholder.typicode.com/users', 取回https://jsonplaceholder.typicode.com/posts' ]。然后[res,res2]=>[res.json,res2.json] 。然后[d1,d2]=>{ 控制台。logd1、d2; }; }; //承诺{[[
( async() => {
const [user, posts] = await Promise.all([
fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json()),
fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json())
]);
console.log(users, posts);
});
})();
然后,您将每个res.json直接链接到它的源承诺,而Promise.all将等待您
仅供参考,我看不出有任何理由在这里使用async/Wait,因为您可以这样做:
Promise.all([
fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json()),
fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json())
]).then( ([users,posts]) => {
console.log(users, posts);
});
仅供参考,一个简单的助手函数似乎也很有用:
function fetchJSON(req) {
return fetch(req).then(res => res.json());
}
然后,你可以做:
Promise.all([
fetchJSON('https://jsonplaceholder.typicode.com/users'),
fetchJSON('https://jsonplaceholder.typicode.com/posts')
]).then( ([users,posts]) => {
console.log(users, posts);
});
而且,您可能希望使用所有这些选项进行错误处理,以便看到错误。您可以使用.catch,也可以使用try/catch来包围您的wait。res.json返回另一个承诺,因此您必须在承诺链中返回该承诺,或者使用wait or.then。因为您使用[res.json,res2.json]作为承诺链中的返回值,所以您将承诺隐藏在该数组中,因此承诺链根本不会等待它们。承诺本身就是回报,因此承诺。所有人都不知道他们在那里,也不等待他们
我建议将每个res.json链接到它自己的父级:
( async() => {
const [user, posts] = await Promise.all([
fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json()),
fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json())
]);
console.log(users, posts);
});
})();
然后,您将每个res.json直接链接到它的源承诺,而Promise.all将等待您
仅供参考,我看不出有任何理由在这里使用async/Wait,因为您可以这样做:
Promise.all([
fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json()),
fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json())
]).then( ([users,posts]) => {
console.log(users, posts);
});
仅供参考,一个简单的助手函数似乎也很有用:
function fetchJSON(req) {
return fetch(req).then(res => res.json());
}
然后,你可以做:
Promise.all([
fetchJSON('https://jsonplaceholder.typicode.com/users'),
fetchJSON('https://jsonplaceholder.typicode.com/posts')
]).then( ([users,posts]) => {
console.log(users, posts);
});
而且,您可能希望使用所有这些选项进行错误处理,以便看到错误。您可以使用.catch,也可以使用try/catch来包围wait。问题在于,返回的是数组而不是承诺。然后[res,res2]=>[res.json,res2.json],因为json响应方法返回另一个承诺。这将导致在接下来的一系列承诺 应该是
async () => {
const [user, posts] = await Promise.all([
fetch('https://jsonplaceholder.typicode.com/users'),
fetch('https://jsonplaceholder.typicode.com/posts')
]).then(responses =>
Promise.all(responses.map(response => response.json()))
);
console.log(user, posts);
}
请注意,添加另一个.then[d1,d2]=>{console.logd1,d2}是不必要的,而且还会导致错误,因为等待的值未定义。问题在于返回的是数组而不是承诺。然后[res,res2]=>[res.json,res2.json],因为json响应方法返回另一个承诺。这将导致在接下来的一系列承诺 应该是
async () => {
const [user, posts] = await Promise.all([
fetch('https://jsonplaceholder.typicode.com/users'),
fetch('https://jsonplaceholder.typicode.com/posts')
]).then(responses =>
Promise.all(responses.map(response => response.json()))
);
console.log(user, posts);
}
请注意,添加另一个.then[d1,d2]=>{console.logd1,d2}是不必要的,而且还会导致错误,因为等待的值未定义。这在@jfriend00中非常有意义。非常感谢。我在为ES6 Hipster编写的JavaScript黑客中看到了这个例子,并认为我应该试试。一个简短的提示。这段代码现在在promise TypeError中返回Uncaught:中间值不是上面提供的Iterablet。@rsilva-上面有四个不同的代码块。[用户、帖子]是否存在问题?或者是哪句话?这句话在@jfriend00中非常有意义。非常感谢。我在为ES6 Hipster编写的JavaScript黑客中看到了这个例子,并认为我应该试试。一个简短的提示。这段代码现在在promise TypeError中返回Uncaught:中间值不是上面提供的Iterablet。@rsilva-上面有四个不同的代码块。[用户、帖子]是否存在问题?或者确切的路线是什么?