简体   繁体   English

我可以从typescript中的promise中返回一个数组列表

[英]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. 我需要在typescript中的promise中返回一个数组列表。 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 . 你不应该使用: any Instead define the actual types of the objects where possible. 而是尽可能定义对象的实际类型。 In addition, you should define the Promise return type like so: 另外,你应该像这样定义Promise返回类型:

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. 一旦你定义了Promise的返回类型,你就会得到关于then的参数的类型信息。

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. 您还可以使用接口来捆绑类型,这样就不会使代码膨胀,特别是如果您不止一次需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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