【问题标题】:Returning Promise says Promise Pending, Node js?返回 Promise 说 Promise Pending,Node js?
【发布时间】:2018-08-19 08:11:08
【问题描述】:

我是 Nodejs 新手,第一次处理 Promise,所以现在的上下文是当我尝试返回 Promise 时,它​​显示状态 Promise 。如何解决它,谁能指导我完成这个?

这是我正在调用将返回一个承诺的函数的代码。粗线显示我想要返回该承诺并存储在对象中的位置。

for(let i = 0; i<responseArray.length; i++){
                    let dollar = {
                            amount : 0
                    };
                    if(i == 1){
                        continue;    
                    }                   
                   dollar.amount = **currenciesService.getCurrencyLatestInfo(responseArray[i].currency);**
                   dollarAmount.push(dollar);
                }
                console.log("$", dollarAmount);

这是一个返回承诺的代码。

const getCurrencyLatestInfo = function(currency) {
return new Promise(function(resolve, reject) {
    request('https://min-api.cryptocompare.com/data/price?fsym='+currency+'&tsyms='+currency+',USD', { json: true }, (err, res, body) => 
    {
        if (err) {
            reject(err);
        } else {
            var result= body;
            resolve(result);
            console.log("RESULT: ",result.USD); 
        }
    });
})

}

【问题讨论】:

  • 输出如下: [ { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } }, { amount: Promise { } } ,{金额:承诺{}},{金额:承诺{}},{金额:承诺{}},{金额:承诺{}}]
  • 很明显返回一个promise会导致调用函数收到一个promise,不是吗?我的意思是,如果你返回true,调用函数就会得到true,等等。无论如何,要获得一个promise的resolved值,需要使用promise的.then方法。
  • 承诺尚未兑现。您需要等待 amount 承诺兑现 .then()await 在使用它的价值之前... developers.google.com/web/fundamentals/primers/…

标签: javascript node.js promise


【解决方案1】:

您需要等待这些承诺解决,然后才能使用已解决的值

这里是对你的循环的一个小的重写,应该可以工作

let promises = [];
for(let i = 0; i<responseArray.length; i++){
    if(i == 1){
        continue;    
    }                   
   let dollar = currenciesService.getCurrencyLatestInfo(responseArray[i].currency)
       .then(amount => ({amount})); // do you really want this?
   promises.push(dollar);
}
Promise.all(promises)
.then(dollarAmount =>console.log("$", dollarAmount))
.catch(err => console.error(err));

这应该会产生一个类似[{amount:123},{amount:234}] 的数组,正如您的代码所期望的那样

上面也可以简化为

Promise.all(
    responseArray
    .filter((_, index) => index != 1)
    .map(({currency}) => 
        currenciesService.getCurrencyLatestInfo(currency)
        .then(amount => ({amount})) // do you really want this?
    )
)
.then(dollarAmount =>console.log("$", dollarAmount))
.catch(err => console.error(err));

注意:您的原始代码建议您希望结果采用{amount:12345} 的形式 - 当您想要 console.log("$", ....) ... 时这似乎很奇怪......因为控制台输出会像

$ [ { amount: 1 }, { amount: 0.7782 } ]

当然给出了两个结果 - 看不到你的 responseArray 所以,我只是在猜测

【讨论】:

  • 感谢您的解决方案,没有待处理的承诺,但没有显示任何输出。这是什么意思?
  • there is nore pending promises - 这是什么意思?你是说console.log("$", dollarAmount) 永远不会被调用吗?也许发生了拒绝
  • 我添加了拒绝处理 - 会被调用吗?
  • 得到了什么?我问了一个问题?!?
  • 它没有被拒绝。我试过你的拒绝处理。我将 resolve 方法放在 setTimeout 方法中,现在它显示未定义的值。这是否意味着我们得到了响应但不是所需的格式?
猜你喜欢
  • 2023-04-01
  • 2021-04-21
  • 2019-12-23
  • 2020-07-09
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多