javascript - ES6 generator functions in angular -


recently started using generators in angular project. here's how far:

function loadposts(skip) {     return $rootscope.spawn(function *() {         try {             let promise = yield user.findall();             $timeout(function () {                 // handle user list             });         } catch (err) {             // handle err         }     }); }  

from i've read next part won't necessary in es7, have spawn function in run block of app.

$rootscope.spawn = function (generatorfunc) {     function continuer(verb, arg) {         var result;         try {             result = generator[verb](arg);         } catch (err) {             return promise.reject(err);         }         if (result.done) {             return result.value;         } else {             return promise.resolve(result.value).then(onfulfilled, onrejected);         }     }     var generator = generatorfunc();     var onfulfilled = continuer.bind(continuer, "next");     var onrejected = continuer.bind(continuer, "throw");     return onfulfilled(); }; 

everything works find way @ moment, thing don't have call $timeout() after each promise. if don't $scope variables initialized inside timeout won't initialized. seems me angular digest system needs triggered manually.

why , there way make cleaner?

i assume because spawn method uses native promises, not angular implementation. try use $q instead:

function continuer(verb, arg) {     var result;     try {         result = generator[verb](arg);     } catch (err) {         return $q.reject(err);     }     if (result.done) {         return result.value;     } else {         return $q.resolve(result.value).then(onfulfilled, onrejected);     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -