Javascript 如何在react native中对来自外部API的数据使用承诺

Javascript 如何在react native中对来自外部API的数据使用承诺,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在从外部api获取数据。该数据为货币对,如欧元兑美元,分别为实时询价和报价,如1.17123和1.17150。因此,应用程序的用户需要输入一个未来价格,这样,如果询问或出价达到用户输入的价格,就会记录一条消息。我尝试过使用承诺,但它会在我运行应用程序时立即输出用户输入到文本输入中的价格,而不是检查价格是否已达到用户预期的未来价格。 下面是我的代码: //hook for the clicked currency pair const [pricealert, setPricealert]

我正在从外部api获取数据。该数据为货币对,如欧元兑美元,分别为实时询价和报价,如1.17123和1.17150。因此,应用程序的用户需要输入一个未来价格,这样,如果询问或出价达到用户输入的价格,就会记录一条消息。我尝试过使用承诺,但它会在我运行应用程序时立即输出用户输入到文本输入中的价格,而不是检查价格是否已达到用户预期的未来价格。 下面是我的代码:

//hook for the clicked currency pair

const [pricealert, setPricealert]  = useState(0)

function checkAlertCondition (){
  return new Promise((resolve, reject) => {
    if(pricealert >= {...data.prices.AskPrice})
    {
      resolve({
        Pair: {...data.prices.instrument},
        message: "Price" + pricealert + "Has been hit"
      });
    } else if (pricealert <= {...data.prices.BidPrice}) {
      resolve({
        Pair:{...data.prices.instrument},
        message: "Price" + pricealert + "has been hit"
      });
    } else {
      reject ("Create Alert")
    }
  });
}

checkAlertCondition()
.then((message) => {
  console.log(message)
  .then((message) => {
    console.log(message)
  })
  .catch(() => {
    console.log(err)
  })
})
      

 <Modal visible={modalopen} animationType={"fade"}>
   <View style={styles.modal}>
     <View>
       <Text style={{textAlign: "center", fontWeight: "bold"}}>
         {data.prices[clickedindex].instrument}
       </Text>
       <Text style={{textAlign: "center"}}>
         {data.prices.AskPrice}/{data.prices.BidPrice}
       </Text>
       <Card.Divider/>
       <View style={{ flexDirection: "row"}}>
         <View style={styles.inputWrap}>
           <TextInput
             style={styles.textInputStyle}
             value={pricealert}
             onChangeText = {(pricealert) => setPricealert(pricealert)}
             placeholder="Alert Price"
             placeholderTextColor="#60605e"
             numeric
             keyboardType='decimal-pad' 
           />
           </View>
         </View>   
         <TouchableOpacity
           style={styles.button}
           onPress={() => {
             if(pricealert.length < 7) {
               Alert.alert("Error", "Enter a valid price")
               return;
             } else if (pricealert.length > 7) {
               Alert.alert("Error", "Enter a valid price")
               return;
             }
             setModalOpen(false);checkAlertCondition()} }
           >
             <Text style={styles.buttonTitle}>OK</Text>
           </TouchableOpacity>
         </View>
       </View>
     </Modal>
       
  
//单击的货币对的挂钩
常量[pricealert,setPricealert]=useState(0)
函数checkAlertCondition(){
返回新承诺((解决、拒绝)=>{
if(pricealert>={…data.prices.AskPrice})
{
决心({
配对:{…数据.价格.仪器},
消息:“价格”+pricealert+“已命中”
});
}否则(价格警报){
console.log(消息)
。然后((消息)=>{
console.log(消息)
})
.catch(()=>{
console.log(错误)
})
})
{data.prices[clickedindex].instrument}
{data.prices.AskPrice}/{data.prices.BidPrice}
setPricealert(pricealert)}
占位符=“通知价格”
占位符textcolor=“#60605e”
数字的
键盘类型=“十进制键盘”
/>
{
如果(pricealert.length<7){
警报。警报(“错误”,“输入有效价格”)
返回;
}否则如果(pricealert.length>7){
警报。警报(“错误”,“输入有效价格”)
返回;
}
setModalOpen(false);checkAlertCondition()}
>
好啊

您可能希望使用
onPress
函数中的承诺,如下所示:

       <TouchableOpacity
         style={styles.button}
         onPress={
           () => {
             if(pricealert.length < 7) {
               Alert.alert("Error", "Enter a valid price")
               return;
             } else if (pricealert.length > 7) {
               Alert.alert("Error", "Enter a valid price")
               return;
             }
             setModalOpen(false);
             checkAlertCondition().then((message) => {
               console.log(JSON.stringify(message));
             })
             .catch(() => {
               console.log(err)
             });
           }
         }
       >
{
如果(pricealert.length<7){
警报。警报(“错误”,“输入有效价格”)
返回;
}否则如果(pricealert.length>7){
警报。警报(“错误”,“输入有效价格”)
返回;
}
setModalOpen(假);
checkAlertCondition()。然后((消息)=>{
log(JSON.stringify(message));
})
.catch(()=>{
console.log(错误)
});
}
}
>

你能提供一个最简单的代码示例吗?我不知道你想要实现什么。这里的代码太多,无法查看你真正想要关注的部分,而且它显然也缺少一堆代码。我觉得你想让我们关注这个
checkAlertCondition
函数,但根本不清楚
的价格在哪里lert
变量来自提供的最小值code@user23424你不想调用
checkAlertCondition。然后(..
onPress
函数内部?我不明白为什么它在外部我已经尝试过了。它记录了
[object object]未定义的