回调函数返回undefined?

时间:2018-01-13 10:26:57

标签: javascript promise

我有两个函数:get()正在调用fetch()。我认为myFirstPromise不会解决,直到fetch()返回响应变量。我究竟做错了什么? fetch()中显然有一些回调,但它在哪个块中返回响应并不重要,对吗?

 this.fetch = () => {
    this.axios.get(this.URL).then((res) => {
      // parse XML
      this.parseString(res.data, (err, result) => {
        var response = new Array()
        // got response. store each member
        for (var i=0; i<result.Messages[0].Message.length; i++) {
          var dateSent = result.Messages[0].Message[i].DateSent[0],
            to = result.Messages[0].Message[i].To[0],
            status = result.Messages[0].Message[i].Status[0],
            message = result.Messages[0].Message[i].Body[0],
            direction = result.Messages[0].Message[i].Direction[0],
            sid = result.Messages[0].Message[i].Sid[0]
          var object = {
            'To' : {N: to},
            'SID' : {S: sid},
            'Status' : {S: status},
            'Message' : {S: message},
            'DateSent' : {S: dateSent},
            'Direction' : {S: direction},
          };
          response.push(object)
        }
        return response
      })
    })
  }

  this.get = () => {
    let myFirstPromise = new Promise((resolve, reject) => {
      resolve(this.fetch())
    });

    myFirstPromise.then((successMessage) => {
      console.log(successMessage)
    });
  }

1 个答案:

答案 0 :(得分:2)

您需要返回并返回并返回。还要确保从parseString返回对异步结果的承诺。因此,在最内层,您需要创建一个:

  this.fetch = () => {
    return this.axios.get(this.URL).then((res) => {
  //^^^^^^
      return new Promise((resolve, reject) => {
  //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
        this.parseString(res.data, (err, result) => {
          var response = new Array()
          for (var i=0; i<result.Messages[0].Message.length; i++) {
            var dateSent = result.Messages[0].Message[i].DateSent[0],
              to = result.Messages[0].Message[i].To[0],
              status = result.Messages[0].Message[i].Status[0],
              message = result.Messages[0].Message[i].Body[0],
              direction = result.Messages[0].Message[i].Direction[0],
              sid = result.Messages[0].Message[i].Sid[0]
            var object = {
              'To' : {N: to},
              'SID' : {S: sid},
              'Status' : {S: status},
              'Message' : {S: message},
              'DateSent' : {S: dateSent},
              'Direction' : {S: direction},
            };
            response.push(object)
          }
          resolve(response)
  //      ^^^^^^^^
        })
      })
    })
  }

  this.get = () => {
    let myFirstPromise = this.fetch()
  //                     ^^^^^^^^^^^^ No need to wrap in promise what already is one

    myFirstPromise.then((successMessage) => {
      console.log(successMessage)
    });
  }

作为旁注,您可以使用更简洁的代码构建响应数组,并使用它调用resolve:

          resolve(result.Messages[0].Message.map(msg => {
            return {
              'To' : {N: msg.To[0]},
              'SID' : {S: msg.Sid[0]},
              'Status' : {S: msg.Status[0]},
              'Message' : {S: msg.Body[0]},
              'DateSent' : {S: msg.DateSent[0]},
              'Direction' : {S: msg.Direction[0]},
            };
          })