2016-08-28 42 views
1

我想從另一個調用獲取promise的函數獲取值,但它返回undefined,我認爲問題是結果不會等待進程從調用函數直到它完成。React Native在獲取數據完成後從promise獲取值

下面是代碼:

var myConnection = require('../components/connection'); 
var RequestToken = React.createClass({ 
getInitialState: function() { 
    return { 
    }; 
}, 
componentDidMount: function(){ 
    AsyncStorage.getItem('token').then((value) => { 
     if(typeof value != null){ 
      this.setState({"token": value}); 
      // call this function 
      this.catchToken(value); 
     } 
    }).done(); 
}, 
catchToken: async function(token){ 

    try{ 
     var URL = "http://someurl.com/"; 
     var params = { 
      token:token 
     } 
     let val = await myConnection.now(URL,params); 

      this.setState({ 
       responseAPI:val 
      }); 
      // returned undefined 
      console.error(this.state.responseAPI); 
    }catch (e){ 
     console.error(e); 
    } 
} 
}); 

和connection.js

function timeout(ms, promise) { 
return new Promise(function(resolve, reject) { 
    setTimeout(function() { 
     reject(new Error("Connection timeout")) 
    }, ms); 
    promise.then(resolve, reject); 
}); 
} 
var myConnection = { 
now: async function(URL,params){ 
    //return "OK"; 
    var formData = new FormData(); 
    for (var k in params) { 
     formData.append(k, params[k]); 
    } 
    timeout(10000, fetch(URL, { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'multipart/form-data' 
     }, 
     body: formData 
    })).then(
     (response) => response.json() 
    ).then((res) => { 
     // result data 
     return res; 
    }).catch((error) => { 
     console.error(error); 
    }).done(); 
} 
}; 
module.exports = myConnection; 

有人能解釋如何得到返回的值函數過程完成後?

感謝

回答

2

您應該處理的部件在這種方式的承諾:你需要(在你的代碼超時功能)返回一個承諾的API側

myConnection.now(URL,params) 
.then(val => { 
    this.setState({ 
     responseAPI:val 
    }); 
}) 
.catch(error =>{ 
    console.error(error); 
}); 

var myConnection = { 
    now: async function(URL,params){ 
    //return "OK"; 
    var formData = new FormData(); 
    for (var k in params) { 
     formData.append(k, params[k]); 
    } 

//You need to return the promise 
    return timeout(10000, fetch(URL, { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'multipart/form-data' 
     }, 
     body: formData 
    })).then(
     (response) => response.json() 
    ).then((res) => { 
     // result data 
     return res; 
    }).catch((error) => { 
     console.error(error); 
    }).done(); 
    } 
}; 

超時應拒絕或解決承諾,並且如果獲取調用位於此處更簡單:

function timeout(ms, promise) { 
    return new Promise(function(resolve, reject) { 
    setTimeout(function() { 
     reject(new Error("Connection timeout")) 
    }, ms); 
    fetch(URL, { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'multipart/form-data' 
     }, 
     body: formData 
    })).then(
     (response) => response.json() 
).then((res) => { 
     // result data 
     resolve(res); 
    }).catch((error) => { 
     reject(error); 
    }).done(); 
    }); 
} 
+0

謝謝,它也適用於AsyncStorage –