我正在玩 ES6 并尝试yield
处理角度请求。var data = yield getData();
没有按照我预期的方式工作。我得到了{"value":{"$$state":{"status":0}},"done":false}
,我想得到{"value":"its working!","done":true}
这是我的代码。
索引.html
<!DOCTYPE html>
<html ng-app="app">
<body ng-controller="bodyCtrl">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.js"></script>
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script>
angular.module('app', []);
angular.module('app')
.controller('bodyCtrl', function ($scope, $http, $q) {
var getData = function () {
var deferred = $q.defer();
$http.get('data.json').then(function (response) {
console.log(response.data.myData);
deferred.resolve(response.data.myData);
});
return deferred.promise;
};
var myGen = function*(){
var data = yield getData();
var two = yield 2;
var three = yield 3;
console.log(data, two, three);
};
var gen = myGen();
console.log(JSON.stringify(gen.next()));
console.log(JSON.stringify(gen.next()));
console.log(JSON.stringify(gen.next()));
console.log(JSON.stringify(gen.next()));
});
</script>
</body>
</html>
数据.json
{"myData": "its working!"}
结果
{"value":{"$$state":{"status":0}},"done":false}
{"value":2,"done":false}
{"value":3,"done":false}
{"done":true}
也非常感谢一些解释!