当前位置:网站首页>19 classic cases of generator functions

19 classic cases of generator functions

2022-06-23 19:00:00 sanda_ nd

1、1s Post output 111,2s Post output 222,3 Second output 333

        //  Constant callbacks like this can lead to a callback hell 
        setTimeout(() => {
            console.log(111)
            setTimeout(() => {
                console.log(222)
                setTimeout(() => {
                    console.log(333)
                }, 3000)
            }, 2000)
        }, 1000)





        
        function one() {
            setTimeout(() => {
                console.log(111)
                iterator.next()
            }, 1000)
        }

        function two() {
            setTimeout(() => {
                console.log(222)
                iterator.next()
            }, 2000)
        }

        function three() {
            setTimeout(() => {
                console.log(333)
                iterator.next()
            }, 3000)
        }

        function* gen() {
            yield one()
            yield two()
            yield three()

        }

        let iterator = gen()
        iterator.next() //  Call directly three times , No delay effect 

2、  Simulated acquisition of user data 、 Order data 、 Commodity data


 

 

原网站

版权声明
本文为[sanda_ nd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231759388927.html