保证typescript编译器不会抱怨

保证typescript编译器不会抱怨,typescript,promise,Typescript,Promise,这可能很容易,但我对承诺还很陌生。我的代码未编译,如何更改: public static sendTestEvent(): Promise<boolean> { let options: any = {headers: {'Content-Type': 'application/json'}}; this.modifyEvent().then(()=> { return WebRequest.post(this.URI, options, JS

这可能很容易,但我对承诺还很陌生。我的代码未编译,如何更改:

public static sendTestEvent(): Promise<boolean> {
    let options: any = {headers: {'Content-Type': 'application/json'}};

    this.modifyEvent().then(()=> {
        return WebRequest.post(this.URI, options, JSON.stringify(this.eventToSend)).then((response) => {
            return browser.sleep(Config.EVENT_PROCESS_TIMEOUT).then(() => {
                console.log('resolved');
                return Promise.resolve(true);
            });
        });
    }, (err)=> {
        return Promise.resolve(false);
    });

}

public static modifyEvent(): Promise<boolean> {
    let currentDate = new Date();

    return new Promise<boolean>((resolve, reject) => {
        console.log('Event modified');
        resolve(true);
    });
}
公共静态sendTestEvent():承诺{
let选项:any={headers:{'Content-Type':'application/json'}};
this.modifyEvent()。然后(()=>{
返回WebRequest.post(this.URI,options,JSON.stringify(this.eventToSend))。然后((response)=>{
返回browser.sleep(Config.EVENT\u PROCESS\u TIMEOUT)。然后(()=>{
console.log('resolved');
返回承诺。解决(真实);
});
});
},(错误)=>{
返回承诺。解决(错误);
});
}
公共静态modifyEvent():承诺{
让currentDate=新日期();
返回新承诺((解决、拒绝)=>{
console.log('Event modified');
决心(正确);
});
}

我得到“非无效返回类型需要返回语句”

您已将该方法声明为返回承诺。那就把它还给我吧:

public static sendTestEvent(): Promise<boolean> {
let options: any = {headers: {'Content-Type': 'application/json'}};

return this.modifyEvent().then(()=> {
//...
公共静态sendTestEvent():承诺{
let选项:any={headers:{'Content-Type':'application/json'}};
返回此.modifyEvent()。然后(()=>{
//...

您已将方法声明为返回承诺。因此只需返回它:

public static sendTestEvent(): Promise<boolean> {
let options: any = {headers: {'Content-Type': 'application/json'}};

return this.modifyEvent().then(()=> {
//...
公共静态sendTestEvent():承诺{
let选项:any={headers:{'Content-Type':'application/json'}};
返回此.modifyEvent()。然后(()=>{
//...

从外观上看,您已经告诉TypeScript您计划在
sendTestEvent
方法中返回类型为
Promise
的内容。该方法中似乎找不到返回语句。您应该能够通过返回
的结果来修复此问题。然后在该方法中返回
方法的结果

public static sendTestEvent(): Promise<boolean> {
    let options: any = {headers: {'Content-Type': 'application/json'}};

    return this.modifyEvent().then(()=> {
//  ^^^^^^ return here
        return WebRequest.post(this.URI, options, JSON.stringify(this.eventToSend)).then((response) => {
            return browser.sleep(Config.EVENT_PROCESS_TIMEOUT).then(() => {
                console.log('resolved');
                return Promise.resolve(true);
            });
        });
    }, (err)=> {
        return Promise.resolve(false);
    });
}

public static modifyEvent(): Promise<boolean> {
    let currentDate = new Date();

    return new Promise<boolean>((resolve, reject) => {
        console.log('Event modified');
        resolve(true);
    });
}
公共静态sendTestEvent():承诺{
let选项:any={headers:{'Content-Type':'application/json'}};
返回此.modifyEvent()。然后(()=>{
//^^^^^^^返回此处
返回WebRequest.post(this.URI,options,JSON.stringify(this.eventToSend))。然后((response)=>{
返回browser.sleep(Config.EVENT\u PROCESS\u TIMEOUT)。然后(()=>{
console.log('resolved');
返回承诺。解决(真实);
});
});
},(错误)=>{
返回承诺。解决(错误);
});
}
公共静态modifyEvent():承诺{
让currentDate=新日期();
返回新承诺((解决、拒绝)=>{
console.log('Event modified');
决心(正确);
});
}

这应该可以解决编译器遇到的问题。

从外观上看,您已经告诉TypeScript您计划在
sendTestEvent
方法中返回类型为
Promise
的内容。该方法中似乎没有找到return语句。您应该可以通过返回
的结果。然后在该方法中使用
方法

public static sendTestEvent(): Promise<boolean> {
    let options: any = {headers: {'Content-Type': 'application/json'}};

    return this.modifyEvent().then(()=> {
//  ^^^^^^ return here
        return WebRequest.post(this.URI, options, JSON.stringify(this.eventToSend)).then((response) => {
            return browser.sleep(Config.EVENT_PROCESS_TIMEOUT).then(() => {
                console.log('resolved');
                return Promise.resolve(true);
            });
        });
    }, (err)=> {
        return Promise.resolve(false);
    });
}

public static modifyEvent(): Promise<boolean> {
    let currentDate = new Date();

    return new Promise<boolean>((resolve, reject) => {
        console.log('Event modified');
        resolve(true);
    });
}
公共静态sendTestEvent():承诺{
let选项:any={headers:{'Content-Type':'application/json'}};
返回此.modifyEvent()。然后(()=>{
//^^^^^^^返回此处
返回WebRequest.post(this.URI,options,JSON.stringify(this.eventToSend))。然后((response)=>{
返回browser.sleep(Config.EVENT\u PROCESS\u TIMEOUT)。然后(()=>{
console.log('resolved');
返回承诺。解决(真实);
});
});
},(错误)=>{
返回承诺。解决(错误);
});
}
公共静态modifyEvent():承诺{
让currentDate=新日期();
返回新承诺((解决、拒绝)=>{
console.log('Event modified');
决心(正确);
});
}

这应该可以解决您在编译器中遇到的问题。

sendTestEvent没有返回任何内容sendTestEvent没有返回任何内容实际上只是部分正确,这里的问题在于的不兼容性。然后,WebRequest.post方法返回了一个承诺,在该方法上删除then解决了问题事实上,这只是部分正确,这里的问题在于的不兼容。然后,WebRequest.post方法返回了一个承诺,在该方法上删除then解决了问题