国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

node.js - Solve the sample code of Generator+Promise in JavaScript that you don't know
某草草
某草草 2017-06-15 09:21:42
0
2
716
function foo(x,y) {
    return request(
        //request是一個(gè)Promise對(duì)象
        "http://some.url.1/?x=" + x + "&y=" + y
    );
}

function *main() {
    try{
        var text = yield foo( 11, 31 );
        //在yield處暫停后 yield需要等待第二次next()傳值 text應(yīng)該沒有被賦值
        console.log( text );
    }
    catch (err) {
        console.error( err );
    }
}

var it = main();

var p = it.next().value;
//等待promise p決議
p.then(
    function (text) {
        it.next( text );
        //這里拿到的 text 應(yīng)該沒有賦到值呀
    },
    
    function (err) {
        it.throw( err );
    }
);

這是你不知道的JavaScript 生成器+Promise 小節(jié)中的一段示例代碼
**其中 text 應(yīng)該拿到的是yield 的值 而yield 應(yīng)該需要第二個(gè)next()去賦值 那么 text應(yīng)該是undefined 這里我就看不懂了 求解!**
某草草
某草草

reply all(2)
左手右手慢動(dòng)作

The next call of the first iterator will be executed to the first yield. At this time, no value is assigned, but an ajax function based on promise is returned.
After resolution of this promise, the return value of the ajax request will be used as a parameter. The form is assigned to the first function in then as a parameter
like this

(() => new Promise((resolve => { resolve("我是參數(shù)"); })))().then(data => console.log(data), err => { throw err; })    //"我是參數(shù)"

Next, this parameter will be assigned to the position of the first yield and the function will be executed

Peter_Zhu

//The text obtained here should not be assigned a value

I misunderstood, text is the result of successful request, I suggest you understand Promise again

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template