javascript React Native 中的 Promise

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51093024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:24:34  来源:igfitidea点击:

Promises in React native

javascriptreactjsreact-nativepromise

提问by Franco Coronel

api/index.js

api/index.js

const URL = 'http://10.0.2.2:5000';
const fetching = false;

export default (type, filter, dateFilter, position) => {
    if(fetching) return Promise.reject(new Error('Request in progress'));
    fetching = true;
    return fetch(URL + `/search/${type}/${filter}/${dateFilter}/${position}/0/0`)
    .then(response => Promise.all([response, response.json()]))
    .catch(err => console.log("error catch search:", err.message))

}

I need to put fetching false so in that way i can call this function again, but i dont know where to put it to make it work. If i create another then like this after the then writen and before the catch like this:

我需要把 fetching 设为 false,这样我就可以再次调用这个函数,但我不知道把它放在哪里才能让它工作。如果我在 then writen 之后和 catch 之前创建另一个 then 像这样:

.then(() => fetching = false)

The problem is that it returns false to the place the function is called and NOT the data this is where is called:

问题是它返回 false 到调用函数的位置,而不是调用这里的数据:

actions/index.js

动作/ index.js

getDataApi(type, filter, dateFilter, position)
   .then(res => {   
     if (res !== false) {         
         if (state.dataReducer.data.length === 0) {
             dispatch(getDataSuccess(res[1]))
         } else {
             dispatch(getDataSuccess(res[1], state.dataReducer.data))
         }       
    }
 })
.catch((err) => console.log(9999, err))

So my problem is that it doesnt enter in the getDataSuccess because is false.I dont know why it cannot send the data to this function and it ends sending the result of fetching = false.

所以我的问题是它没有进入getDataSuccess,因为它是false。我不知道为什么它不能将数据发送到这个函数并且它结束发送fetching = false的结果。

采纳答案by CertainPerformance

You need another .thenso that you can reassign fetchingafter the response.json()resolves. You should also probably reassign fetchingin the catchso that future requests are possible even if there's an error once, and return falsein the catchso the .thenafter getDataAPIwill properly ignore failed requests. Also, use letrather than constfor fetching:

您需要另一个,.then以便您可以fetchingresponse.json()解决后重新分配。你也应该可能重新分配fetchingcatch,这样将来的请求是可能的,即使有一次的错误,并且return falsecatch这样的.thengetDataAPI将会正确忽略失败的请求。另外,使用let而不是constfor fetching

const URL = 'http://10.0.2.2:5000';
let fetching = false;

export default (type, filter, dateFilter, position) => {
  if (fetching) return Promise.reject('Request in progress');
  fetching = true;
  return fetch(URL + `/search/${type}/${filter}/${dateFilter}/${position}/0/0`)
    .then(response => Promise.all([response, response.json()]))
    .then(([response, responseObj]) => {
      fetching = false;
      return [response, responseObj];
    })
    .catch(err => {
      console.log("error catch search:", err.message);
      fetching = false;
      // Choose one, depends what you need.
      return false; // If you want to ignore the error and do something in a chained .then()
      return Promise.reject(err); // If you want to handle the error in a chained .catch()
    })
}