I have been experimenting with ES6 and attempting to use yield
in conjunction with an angular request. However, I am encountering some unexpected behavior. When I write var data = yield getData();
, the output is not what I had anticipated. Instead of receiving
{"value":"its working!","done":true}
, I am getting {"value":{"$$state":{"status":0}},"done":false}
Let me share my code with you.
index.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>
data.json
{"myData": "its working!"}
Result
{"value":{"$$state":{"status":0}},"done":false}
{"value":2,"done":false}
{"value":3,"done":false}
{"done":true}
If anyone could provide a brief explanation, it would be greatly appreciated!