簡體   English   中英

如何在React Native中的fetch函數中設置超時

[英]How to set timeout in the fetch function in react native

我有以下提取功能:

RequestLogin(params){
 let {username,password}=params;
 try{
   let response = fetch('URL',{
     method:'POST',
     headers:{
      'Accept' : 'application/json',
      'Content-Type' : 'application/json'
     },   
     body:JSON.stringify({
      username,
      password
     })
    }).then(response => response.json()).then(responseJson=> {
       console.log(responseJson);
     })
    } catch(error){
       console.log(error);
    }

我想知道如何設置超時時間?

謝謝,提前。

您可以做出可以awaited的承諾

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

當你打電話function ,它使用一個fetch function為你提供以上

// Inside some async wrapped func
    await timeout(4000);
    await RequestLogin(params)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM