简体   繁体   中英

Can I return a list of arrays from a promise in typescript

I need to return a list of arrays in a promise in typescript. This is my implementation and it's not working out. I need serious help on this one.

const promise = new Promise((resolve, reject) => {    
    let onbidJobs:any = [...objects];
    let awardedJobs:any = [...objects];
    let completedJobs:any = [...objects];

    //return the jobs
    resolve({
        onbid: onbidJobs,
        awarded: awardedJobs,
        completed: completedJobs
    });
});

let returnedonbid:any;
let returnedaward:any;

promise.then((resolve) => {
   this.returnedaward = resolve.awarded;
   this.returnedonbid = resolve.onbid;  //<-- I really want to return my arrays like this
});

You should not use : any . Instead define the actual types of the objects where possible. In addition, you should define the Promise return type like so:

const promise = new Promise<{onbid:any, awarded:any, completed:any}>((resolve, reject) => { ...

Once you define the Promise 's return type, you'll get type info on the then 's argument.

You can also use an interface to bundle the type so it doesn't bloat your code, especially if you need it more than once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM