将响应传递给$ q promise的第二个`then`

Mic*_*ael 1 javascript angularjs q

看看这个简单的例子:

function colorPromise() {
  return $q.when({data:['blue', 'green']})
}

function getColors() {
  return colorPromise().then(function(res) {
     console.log('getColors', res)
     // do something with res
  };
}

function testGetColors() {
  getColors().then(function(res) {
    if (angular.equals(res, {data:['blue', 'green']})) {
      console.log('passes')
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

Plunker:http://plnkr.co/edit/LHgTeL9sDs7jyoS7MJTq?p = preview
在这个例子res中的testGetColors功能是undefined.

你怎么能传递res到承诺的第二个then功能$q

Jam*_*iec 5

你需要回来 res

function getColors() {
  return colorPromise().then(function(res) {
     console.log('getColors', res)
     return res; // here
  };
}
Run Code Online (Sandbox Code Playgroud)