2017-07-28 50 views
0

我的應用程序顯示加載時顯示在第一個屏幕上的問題列表。承諾中未找到可調用簽名(Flow,React Native,JS)

我目前正在嘗試實施它,以便它能夠監聽AppState,並在它再次變爲「活動」時再次刷新問題列表,以確保列表是最新的。

我目前使用MobX來跟蹤我的狀態。出現

一切是做工精細,但流動產生了以下錯誤......

Library type error: 
/private/tmp/flow/flowlib_1950abc4/core.js:666 
666:  onFulfill?: (value: R) => Promise<U> | U, 
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. Callable signature not found in 
17:    .then(this.refreshQuestions()) // eslint-disable-line promise/prefer-await-to-then 
         ^^^^^^^^^^^^^^^^^^^^^^^ Promise. See: packages/mobile/src/questionList/QuestionRepository.js:17 

packages/mobile/src/questionList/QuestionRepository.js:16 
      v---------------------------- 
16:   this.loadQuestionsFromCache() 
17:    .then(this.refreshQuestions()) // eslint-disable-line promise/prefer-await-to-then 
       -----------------------------^ call of method `then` 
666:  onFulfill?: (value: R) => Promise<U> | U, 
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. Callable signature not found in. See lib: /private/tmp/flow/flowlib_1950abc4/core.js:666 
17:    .then(this.refreshQuestions()) // eslint-disable-line promise/prefer-await-to-then 
         ^^^^^^^^^^^^^^^^^^^^^^^ Promise 

這是我的構造和正在調用相關的方法...

constructor() { 
     this.loadQuestionsFromCache() 
      .then(this.refreshQuestions()) // eslint-disable-line promise/prefer-await-to-then 
      .then(this.startListeningForStateChanges()) // eslint-disable-line promise/prefer-await-to-then 
      .catch(() => { 
       Alert.alert(
        'Ooops!', 
        'Something went wrong when I tried to load everyones questions :(please try refreshing me', 
        [{text: 'OK'}], 
        {cancelable: false} 
       ) 
      }) 
    } 

    @action('List is refreshing (true/false) updated') 
    updateIsListRefreshing(isRefreshing: boolean) { 
     this.isListRefreshing = isRefreshing 
    } 

    @action('Set question list') 
    setQuestions(questions: Question) { 
     this.questions = questions 
    } 

    handleAppStateChange = (nextAppState: string) => { 
     if (nextAppState === 'active') { 
      this.refreshQuestions() 
     } 
    } 

    startListeningForStateChanges() { 
     AppState.addEventListener('change', this.handleAppStateChange) 
    } 

    // eslint-disable-next-line complexity, max-statements 
    async refreshQuestions() { 
     if (this.isListRefreshing) { 
      return 
     } 

     try { 
      this.updateIsListRefreshing(true) 

      const response = await fetch(serverURL) 

      if (response.status === 200) { 
       const questionsText = await response.text() 

       this.setQuestions(JSON.parse(questionsText).map(Question.of)) 
       await AsyncStorage.setItem('questions', questionsText) 
      } 

      this.updateIsListRefreshing(false) 
     } catch (error) { 
      this.updateIsListRefreshing(false) 
     } 
    } 

    async loadQuestionsFromCache() { 
     const questionsText = await AsyncStorage.getItem('questions') 

     if (questionsText) { 
      this.setQuestions(JSON.parse(questionsText).map(Question.of)) 
     } 
    } 

回答

0
constructor() { 
    this.loadQuestionsFromCache() 
     .then(this.refreshQuestions) // eslint-disable-line promise/prefer-await-to-then 
     .then(this.startListeningForStateChanges) // eslint-disable-line promise/prefer-await-to-then 
     .catch(() => { 
      Alert.alert(
       'Ooops!', 
       'Something went wrong when I tried to load everyones questions :(please try refreshing me', 
       [{text: 'OK'}], 
       {cancelable: false} 
      ) 
     }) 
} 

@action.bound 
updateIsListRefreshing(isRefreshing: boolean) { 
    this.isListRefreshing = isRefreshing 
} 

@action('Set question list') 
setQuestions(questions: Question) { 
    this.questions = questions 
} 

handleAppStateChange = (nextAppState: string) => { 
    if (nextAppState === 'active') { 
     this.refreshQuestions() 
    } 
} 

@action.bound 
startListeningForStateChanges() { 
    AppState.addEventListener('change', this.handleAppStateChange) 
} 

@action.bound 
async refreshQuestions() { 
    if (this.isListRefreshing) { 
     return 
    } 

    try { 
     this.updateIsListRefreshing(true) 

     const response = await fetch(serverURL) 

     if (response.status === 200) { 
      const questionsText = await response.text() 

      this.setQuestions(JSON.parse(questionsText).map(Question.of)) 
      await AsyncStorage.setItem('questions', questionsText) 
     } 

     this.updateIsListRefreshing(false) 
    } catch (error) { 
     this.updateIsListRefreshing(false) 
    } 
} 
+0

你能解釋一下嗎? – Swards

相關問題