【问题标题】:Reusing Promise creation重用 Promise 创建
【发布时间】:2017-11-02 03:19:26
【问题描述】:

我正在尝试使用不同的 URL 调用 getPromise 函数以返回不同的 Promise,但在第二个 Promise 的 then Success 函数中未定义。

var http=require('http');
var URL='http://localhost:3000';

var getPromise=function(url){
    var promise=new Promise(function(resolve,reject){
        http.get(url,function(response){
            if(response.statusCode < 200 || response.statusCode > 299){
                reject(new Error('ErrorCode '+response.statusCode))
            }
            var result="";
            response.on('data',function(chunk){result +=chunk;} )
            response.on('end',function(){resolve(result);} )
        })
    });
   return promise;
}



getPromise(URL+'/olympic/2016/ranking/4')
      .then(function(data){
         console.log("Response "+JSON.parse(data).Country);
         getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
      })
      .then(function(data){
        console.log("Data "+data)
      })
      .catch(function(err){
         console.log(err)
      });

【问题讨论】:

  • 不应该返回第二个getPromise(...)吗?
  • 开始使用async/await,一切都清楚

标签: javascript node.js promise es6-promise


【解决方案1】:

您必须为下一个“then”返回数据才能接收它。

getPromise(URL+'/olympic/2016/ranking/4')
      .then(function(data){
         console.log("Response "+JSON.parse(data).Country);
         return getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
      })
      .then(function(data){
        console.log("Data "+data)
      })
      .catch(function(err){
         console.log(err)
      });

【讨论】:

    【解决方案2】:

    确保从 promise then 返回数据:

    getPromise(URL+'/olympic/2016/ranking/4')
      .then(function(data){
         console.log("Response "+JSON.parse(data).Country);
         return getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
      })
      .then(function(data){
        console.log("Data "+data)
      })
      .catch(function(err){
         console.log(err)
      });
    

    无论您从then 回调返回什么,都将进一步向下传递到承诺链。现在你没有返回任何东西,因此隐式返回undefined

    【讨论】:

    • 添加 return 后它可以工作,但我不明白这一点,不是调用 getPromise 已经返回了承诺。
    • 再一次:无论你返回 from then 回调,都会传递给下一个 then 回调。所以如果你想让getPromise返回的promise在下一个then回调中可用,你需要从前一个回调中返回它。
    • 我认为调用 getPromise 实际上是将承诺返回到下一个。谢谢,现在知道了
    【解决方案3】:

    你可以改变功能

    getPromise(URL+'/olympic/2016/ranking/4')
          .then(function(data){
             console.log("Response "+JSON.parse(data).Country);
             getPromise(URL+'/iso/country/'+JSON.parse(data).Country)
             .then(function(data){
               console.log("Data "+data)
             });
          })
    
          .catch(function(err){
             console.log(err)
          });
    

    【讨论】:

      【解决方案4】:

      您缺少return

      getPromise(URL+'/olympic/2016/ranking/4')
        .then(function(data){
           console.log("Response "+JSON.parse(data).Country);
      
           /////////// here
           return getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
        })
        .then(function(data){
          console.log("Data "+data)
        })
        .catch(function(err){
           console.log(err)
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-21
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2020-02-14
        相关资源
        最近更新 更多