Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use yield of javascript

Shulou Source: shulou.com Published: 2022-06-01 10:31:03 11月12日 Update

In this article, the editor introduces in detail "how to use javascript's yield". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use javascript's yield" can help you solve your doubts.

Javascript yiel Code:

Function delay (time) {

Return new Promise (function (resolve, reject) {

SetTimeout () = > {

Resolve ()

}, time)

});

}

Function* output () {

Yield delay (500) .then (() = > console.log (1))

Yield (function () {console.log (2); return 'aa'}) ()

Yield delay 200.then (() = > console.log (3))

Yield (function () {console.log (4); return 'aa'}) ()

}

Function runGen (gen, next) {

Const item = gen.next ()

If (item.done) return

Const {value} = item

If (value instanceof Promise) {

Value.then (() = > runGen (gen))

} else {

RunGen (gen)

}

}

RunGen (output ())

After reading this, the article "how to use javascript's yield" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Articles content proper code ideas new knowledge more steps knowledge knowledge points articles details industry information information channels follow channels processing learning practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei MySQL Docker Shulou Information