I am coding a function that return a promise with a given type of data.
My entity name is Groups
and i want to return a array of Groups Groups[]
This is my function
async filterGroupsByAvailableSpots (groups: Groups[] ) :Promise<Groups[]> {
let groupsAvailableToMatch : Groups[] = [] ;
let searchAvailableGroupsLoop = new Promise((resolve, reject) => {
groups.forEach((group, index, array) => {
this.getStudentsByActiveGroup (group.id)
.then((students) => {
//If the group have spots to match
if (students.length < 6) {
groupsAvailableToMatch.push(group)
}
// Stop FLag
if (index === array.length -1) {
resolve(groupsAvailableToMatch);
}
})
});
});
return searchAvailableGroupsLoop;
}
But Im facing an error in the return promise
let searchAvailableGroupsLoop: Promise<unknown>
Type '{}' is missing the following properties from type 'Groups[]': length, pop, push, concat, and 26 more.ts(2740)
I am coding a function that return a promise with a given type of data.
My entity name is Groups
and i want to return a array of Groups Groups[]
This is my function
async filterGroupsByAvailableSpots (groups: Groups[] ) :Promise<Groups[]> {
let groupsAvailableToMatch : Groups[] = [] ;
let searchAvailableGroupsLoop = new Promise((resolve, reject) => {
groups.forEach((group, index, array) => {
this.getStudentsByActiveGroup (group.id)
.then((students) => {
//If the group have spots to match
if (students.length < 6) {
groupsAvailableToMatch.push(group)
}
// Stop FLag
if (index === array.length -1) {
resolve(groupsAvailableToMatch);
}
})
});
});
return searchAvailableGroupsLoop;
}
But Im facing an error in the return promise
let searchAvailableGroupsLoop: Promise<unknown>
Type '{}' is missing the following properties from type 'Groups[]': length, pop, push, concat, and 26 more.ts(2740)
Share
asked Oct 18, 2020 at 1:27
Anderson LaverdeAnderson Laverde
3952 gold badges5 silver badges16 bronze badges
3
-
(∩⌣̀_⌣́) that async looks suspicious, you're not using
await
inside your function, try not to mix them blog.logrocket./async-await-in-typescript – diedu Commented Oct 18, 2020 at 1:56 - stackoverflow./questions/54385676/… – diedu Commented Oct 18, 2020 at 2:03
- did you solve your problem? – diedu Commented Oct 23, 2020 at 1:10
2 Answers
Reset to default 2I think TS is not able to infer the type for the returned variable, try specifying the type when creating the promise
let searchAvailableGroupsLoop = new Promise<Groups[]>((resolve, reject) => {
You need to resolve your Promise for every case. If in your groups array there are none "Stop flags" your promise resolves to {}
. So resolve with an empty array or reject with an error if that suits your requirements better.
async filterGroupsByAvailableSpots (groups: Groups[] ) :Promise<Groups[]> {
let groupsAvailableToMatch : Groups[] = [] ;
let searchAvailableGroupsLoop = new Promise((resolve, reject) => {
groups.forEach((group, index, array) => {
this.getStudentsByActiveGroup (group.id)
.then((students) => {
//If the group have spots to match
if (students.length < 6) {
groupsAvailableToMatch.push(group)
}
// Stop FLag
if (index === array.length -1) {
resolve(groupsAvailableToMatch);
}
})
});
resolve([]); // add this line or reject("Error");
});
return searchAvailableGroupsLoop;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744911066a4600552.html
评论列表(0条)