在没有“ then”方法的情况下获取数据/直接获取和使用

时间:2018-12-26 07:18:23

标签: json react-native

我有Json Url,我从中获取了jsron并导出到其他js文件..

之后,当我要使用它时,我必须使用

js1:

export const getData = () => fetch(state._WEB_SERIVICE_URL+'get_config')
  .then(response => response.json())

js2:

export const B2 = getData().then(data => {
    alert(data.main_color+"*");
    return data.main_color 
      /* do what you want to do in promise resolve callback function */
})

js3

B2.then(data => {
      alert(data+"*2"); // I want to get it directly...
})

如何在不使用.then(..

的情况下访问B2.data

感谢进阶..

1 个答案:

答案 0 :(得分:0)

您可以为此使用await

const response = await fetch(state._WEB_SERIVICE_URL+'get_config')
console.log(response)
return response

有关更多信息,您可以检查文档Networking