A
A
Andrey Kostyuchenko2017-06-22 17:25:33
JavaScript
Andrey Kostyuchenko, 2017-06-22 17:25:33

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

4 answer(s)
O
Oleg, 2017-06-22
@politon

https://developer.mozilla.org/en/docs/Web/JavaScript...

I
Islam Ibakaev, 2017-06-22
@devellopah

read the ES6 Generators In Brief section

O
ozknemoy, 2017-06-23
@ozknemoy

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))

someone will object that generators can and should sequentially resolve several asynchronous requests. for this there is Promise.all
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()
        });

A
Andrey Kostyuchenko, 2017-07-05
@AndKost

Good article with examples
https://habrahabr.ru/post/182620/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question