【问题标题】:Use a promise result from a fetch-api in an if statement of another function - JS在另一个函数的 if 语句中使用来自 fetch-api 的承诺结果 - JS
【发布时间】:2021-09-05 01:11:42
【问题描述】:

所以我有这个 fetch-api

let test = () => fetch(usd_api).then(response => response.json())
.then(data => data.exchange_rates.dash_usd);

控制台日志

let console_test = () => {console.log(test())}

如何在下面150所在的函数中使用这个数字[[PromiseResult]]: 134.445...

function main(){

         fetch(txs_api).then(response => response.json()).then(function(data) {
                
             var amount = data.txs[0].vout[1].value;
               
             if(amount == 150){
                // SUCCESS!
                $('#modal').modal('hide');
                console.log('requests stopped');
                clearInterval(interval);
             }
         })
}

【问题讨论】:

  • @TusharShahi 它给了我一个错误test2:62 Uncaught ReferenceError: console_test is not defined at HTMLButtonElement.onclick
  • fetch 已经返回了一个承诺,不需要包装承诺构造函数。
  • 使用async function console_test () {console.log(await test())}
  • 您可以使用Promise.all 进行两个并行API 调用并在一个数组中获取两个数值:Promise.all([fetch(...), fetch(...)]).then(...)

标签: javascript promise fetch-api


【解决方案1】:
let test = () => fetch(usd_api)
  .then(response => response.json())
  .then(data => data.exchange_rates.dash_usd);

你也想继续使用相同的承诺,如果你想记录你的承诺的结果,你需要等待它完成:

// with .then()
test().then(console.log);

// or with await if you can:
console.log(await test());

【讨论】:

  • 我觉得你需要test().then(console.log);
  • @AlphaHowl 哦,是的,绝对
  • 好的,这很酷,但是如何使用test() 返回值在confirm_transaction 函数中执行if 语句?
  • 顺便说一句这不起作用我不知道为什么console.log(await test());
【解决方案2】:

我想通了。不知道这是否是最好的方法,但它正在工作。

function main(){

  fetch(usd_api).then(response => response.json()).then(function(data) {
                        
      var dash_price = data.exchange_rates.dash_usd;
                    
      fetch(txs_api).then(response => response.json()).then(function(data) {
                    
          var amount = data.txs[0].vout[1].value;

          if(amount == dash_price){
              // SUCCESS!
              $('#modal').modal('hide');
              console.log('requests stopped');
              clearInterval(interval);
          }
      })
   })
}

【讨论】:

    【解决方案3】:

    不返回值。 因为它返回到下面的函数

    function(data) {
      return data.exchange_rates.dash_usd
    }
    

    如果要将值返回给变量,则必须使用 Promise,如下所示。

        let test = new Promise((resolve, reject) => {
            fetch(usd_api).then(response => response.json())
                .then(function (data) {
                    resolve(data.exchange_rates.dash_usd) 
                });
        })
        
        async function demo(){
            let console_test  = await test
            console.log(console_test )
        }
        demo()
    

    注意:不要忘记使用 asyncawait

    【讨论】:

    • @Bergi。这是为了演示。如果您不使用获取。您只是在使用任何需要时间的计算。你可以使用承诺。
    • OP 已经使用了 Promise 和 fetch。他们的test 代码正在运行。你不应该使用new Promise
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2011-03-23
    • 1970-01-01
    • 2022-12-04
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多