Answer the question
In order to leave comments, you need to log in
Applications of generator and promise?
Good day.
I study JS and apply it in practice.
I know separately what a generator is and how it works, there are a lot of articles on this topic and everything is described in detail.
I know what promises are.
The question is how do they work together, where in practice do you need to put these yield or where to return the state of the promise?
Point to a good source or write a couple of examples.
Answer the question
In order to leave comments, you need to log in
I indulged in my time with generators for the frontend. even put it in production. and forgot about it. forever and ever. thing of dubious necessity. well, without promises , it will
return 4 anywhere in a second
function getPromise() {
return new Promise((resolve,fail)=> {
// что либо асинхронное
setTimeout(()=>resolve(4),1e3)
})
}
getPromise().then((d)=>console.log(d))
Promise.all([
httpFactory.get(`reward/${idReward}/view`)
.then(d=> {
this.reward = d.data;
this.term = dictFactory.termsDelivery[this.reward.delivery_rules].name;
this.reward.date = handleDataFactory.getIntervalDate(this.reward.send_date_from,this.reward.send_date_to);
// осталось наград
// sell_count == 0 это бесконечность наград
this.amountRewD=this.reward.sell_count-(this.reward.collected_count || 0)
}),
httpFactory.getAuth()
.then(d=> {
// возвращает 'no' если нет регистрации или она устарела
// под логином возвращает число денег на счету начиная от 0
this.isAuth = d.data;//===false? false:d.data;
// если фронт под логинон а сервер нет, то
if (this.isAuth == 'no' && authFront) userFactory._reLogin(true);
})
]).then(d=> {
this.getAmountRewD()
});
Good article with examples
https://habrahabr.ru/post/182620/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question