【发布时间】: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