Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过函数传递承诺会产生本机反应_Javascript_Reactjs_React Native_Ecmascript 6_Fetch - Fatal编程技术网

Javascript 通过函数传递承诺会产生本机反应

Javascript 通过函数传递承诺会产生本机反应,javascript,reactjs,react-native,ecmascript-6,fetch,Javascript,Reactjs,React Native,Ecmascript 6,Fetch,我对react native是全新的,我一直在浏览代码片段,对承诺是如何传递的感到困惑 我有一个事件处理程序onRefresh(),当我下拉平面列表时调用它,我试图让它在返回true/false时使用apisarchdb的返回 onRefresh = () => { this.setState({...}, () => { return this.apiSearchDB() .then(function(response) { console.

我对react native是全新的,我一直在浏览代码片段,对承诺是如何传递的感到困惑

我有一个事件处理程序
onRefresh()
,当我下拉平面列表时调用它,我试图让它在返回true/false时使用
apisarchdb
的返回

onRefresh = () => {
  this.setState({...}, () => {
    return this.apiSearchDB()
      .then(function(response) {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      });
  })
}

apiSearchDB = () => {
    return fetch('/some_endpoint')
     .then((response) => response.json())
     .then((json) => {
       this.setState({
          ...
       }, () => {return true})
       return true;
    }).catch((error) => {
       console.error(error);
       return false;
    })
 }
console.log(响应)只打印
未定义的
,我不知道为什么

我的处理程序也可以写成

onSearch = () => {
   return new Promise((resolve, reject) => {
      var response = this.apiSearchDB();
       response
          ? resolve();
          : reject();
        }
   });
 }
或者
onSearch=()=>{…}
函数onSearch(){…}


提前谢谢你

您应该阅读更多关于使用(好文章-)的内容。但是,在这种情况下有两条基本规则可以帮助您:

  • 从承诺返回的值被包装在承诺中
  • 承诺可以被束缚
apiSearchDB
应返回一个承诺,其中包含
json
作为解析值,以及
error
作为拒绝值:

apiSearchDB = () =>
  fetch('/some_endpoint')
    .then((response) => response.json())
    .then((json) => json)
    // can be removed unless you want to do something with the error before passing it on
    .catch((error) => Promise.reject(error));
onRefresh
(或
onSearch
)方法应该从
apiSearchDB
获得承诺,并添加自己的链。应该使用
然后
处理程序处理Resolve承诺。如果它是被拒绝的值,它将由
catch
处理程序处理:

onRefresh = () =>
  this.apiSearchDB()
    .then((response) => {
      console.log(response);

      // do something with response          
      this.setState({...}, () => {

      });

      return response;
    })
    .catch((error) => {
      console.log(error);

      // do something with error          
      this.setState({...}, () => {

      });
    });
}