2016-10-04 3 views
0

これはおそらく簡単ですが、私は約束を新しくしています。私のコードはそれを変更する方法を、コンパイルされていません。typescriptコンパイラがPromiseで文句を言わないようにする

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); 
    }); 
} 

私はあなたが約束を返すようにする方法を宣言した

+0

sendTestEventは何も返されません –

答えて

2

を、それはあなたがsendTestEvent方法でタイプPromise<resolving to boolean>の何かを返すことを計画している、あなたが言った活字体のように見えるものから。そのメソッドでreturnステートメントが見つからないようです。その方法で.thenメソッドの結果を返すことでこれを解決できるはずです。

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); 
    }); 
} 

コンパイラで問題が解決するはずです。

+1

実際には部分的にしか真実ではありませんでしたが、WebRequest.postメソッドはPromiseを返しました>、そのメソッドを削除すると、問題 –

1

「Returnステートメントがvoid以外の戻り値の型のために必要とされる」得ます。だからそれを返す:

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

return this.modifyEvent().then(()=> { 
//... 
関連する問題