[英]Async/await not returning result at all
I don't really get the async/await method after trying to ask my lecturer and trying to surf on the web.在尝试询问我的讲师并尝试在 web 上冲浪后,我并没有真正得到 async/await 方法。 I initially thought async/await can return me the desired outcome for my activity selection challenge, but it doesn't.
我最初认为 async/await 可以为我的活动选择挑战返回所需的结果,但事实并非如此。
From this code given, i am trying to console.log()
out the result from the function selectPerformanceByFestivalId
, but then when i see the result, it only gives me an empty object as the result instead of an array of objects.从给出的这段代码中,我试图
console.log()
从 function selectPerformanceByFestivalId
中取出结果,但是当我看到结果时,它只给了我一个空的 object 作为结果而不是对象数组。
By right i was supposed to get something like this:按道理我应该得到这样的东西:
[
{
"performanceid": "1234567890",
"starttime": 1330,
"endtime": 1530,
"festivalid": "2234567891"
},
{
"performanceid": "1234567891",
"starttime": 1530,
"endtime": 1630,
"festivalid": "2234567891"
},
{
"performanceid": "1234567892",
"starttime": 1330,
"endtime": 1530,
"festivalid": "2234567891"
},
{
"performanceid": "1234567893",
"starttime": 1330,
"endtime": 1530,
"festivalid": "2234567891"
},
{
"performanceid": "1234567894",
"starttime": 1430,
"endtime": 1530,
"festivalid": "2234567891"
}
]
But i got this as the result: {}
.但我得到了这个结果:
{}
。
Here is my current async/await code:这是我当前的异步/等待代码:
async function sortPerformanceByFinishTime(endTime) {
const filteredPerformance = await selectPerformanceByFestivalId; //do a await to let the previous function execute first before continuing
const storeArray = [] //create a new array to reorder the stuff again
console.log(filteredPerformance);
};
This is how i did my selectPerformanceByFestivalId
:这就是我做
selectPerformanceByFestivalId
的方式:
// 1. selectPerformance to correctly select set of performance for computation
function selectPerformanceByFestivalId(performances) {
const l = performances.length; //length of performances
const selectedPerformance = []; //create a new array selectedPerformance
for (let i = 0; i < l; i++) { //iterate through all the festivalId
selectedPerformance.push(performances[i]); //push filtered performance into the array
};
return selectedPerformance; //return the array
};
selectPerformanceByFestivalId
is supposed to hold the entire json result(which was the data above) and when i console.log(filteredPerformance)
, i expected the same json result. selectPerformanceByFestivalId
应该保存整个 json 结果(这是上面的数据),当我console.log(filteredPerformance)
时,我希望得到相同的 json 结果。
Is there anything wrong?有什么问题吗?
You are giving a reference to a function object instead of calling it.您正在引用 function object 而不是调用它。
Change your line of code to将您的代码行更改为
const filteredPerformance = await selectPerformanceByFestivalId();
I created a Plunk at https://plnkr.co/edit/OeJg8ISk8XHinuR3 using the example code that you have shared above.我使用您在上面共享的示例代码在https://plnkr.co/edit/OeJg8ISk8XHinuR3创建了一个 Plunk。 Couple of changes to your existing code.
对现有代码的一些更改。
selectPerformanceByFestivalId
.selectPerformanceByFestivalId
之前添加了 async 关键字。selectPerformanceByFestivalId
in the function sortPerformanceByFinishTime
sortPerformanceByFinishTime
中调用 function selectPerformanceByFestivalId
后添加括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.