,所以我有这个fetch-api
let test = () => fetch(usd_api).then(response => response.json())
.then(data => data.exchange_rates.dash_usd);控制台日志
let console_test = () => {console.log(test())}

如何在下面的函数中使用这个数字[[PromiseResult]]: 134.445...,其中的数字是150。
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);
}
})
}发布于 2021-06-21 13:07:01
我想通了。不知道这是不是最好的方法,但它是有效的。
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);
}
})
})
}发布于 2021-06-21 11:07:49
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());发布于 2021-06-21 11:27:56
不要返回值。因为它返回到下面的函数
function(data) {
return data.exchange_rates.dash_usd
}如果您想将您的值返回给一个变量,您必须使用承诺,如下所示。
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()await 注释:不要忘记使用异步和
https://stackoverflow.com/questions/68066646
复制相似问题