How to use yield of javascript
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.