gpt4 book ai didi

javascript - 收到的 promise 待定

转载 作者:行者123 更新时间:2023-11-30 20:18:55 26 4
gpt4 key购买 nike

这段代码返回一个未决的 promise 我想将从 insta 函数返回的数据存储到数据数组中。

cs2.js:- 
async function insta(mode, coin) {
coin = coin.toLowerCase();
var price;
var quantity;
await new Promise(function (resolve, reject) {
request.get(url, function (err, response, body) { //requests to api
a = (JSON.parse(body))['rates']; //converts body to JSON
for (i = 0; i < a.length; i++) {
if (a[i].coin == coin) {
price = parseFloat(price = a[i].rate).toFixed(2); // assigns price
}
}
quantity = (JSON.parse((JSON.parse(body))['volume']['value'])['INR'][coin.toUpperCase()]).toFixed(3); //assigns qunatiity
resolve();
});
});
return [price, quantity, "Insta"];

}

function p2pSignal(coin, mode, noOfP2p, p2pList, message) {
data = [];
data.push(insta(mode, coin)); // I want to store the data in data array
console.log(data);
//setTimeout(function(){console.log(data);},3000)
}

p2pSignal("XRP", "SELL", 4, 'INSTA', "1"); // CALLING THE FUNCTION

命令行代码:-

node cs2.js
[ Promise { <pending> } ]

最佳答案

使用 async 函数的函数应该实现 promise 控制流,例如也是 async:

async function p2pSignal(coin, mode, noOfP2p, p2pList, message) {
data = [];
data.push(await insta(mode, coin));
console.log(data);
}

关于javascript - 收到的 promise 待定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51658659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com