简体   繁体   中英

Why console.logs in generator appears in such order?

Having the code below, I'd like to ask 2 questions (main and secondary):
1. promise p is yielded before console.log(second) in generator function, so why console.log(second) logged before console.log from given promise?
2. I saw somewhere (it was done in koa framework) that returned value (see return 'some value' ) was used in code (sent with the response back to client)... How could I console.log it in my gen function? Is it even possible?

var p = new Promise((resolve) => {
    resolve('should go first?');
})

var resp = p.then((first) => {
    console.log(first);
    return 'some value'; //pertain to the secondary question
})


function* gen(prom){
    var second = yield prom;
    console.log(second);
}

var it = gen(resp);

it.next();
it.next('should go second?');

Question 1.

  1. The executor for promise p resolves the promise synchronously during call with value "should go first".

  2. resp is set to the promise returned by calling then on p . Note the onFulfilled function supplied to a promise is never executed in the same thread which supplies it. If the promise is already resolved, the callback will be scheduled in its own thread after the current thread completes execution.

  3. it is set as the iterator of the generator function. Code is still executing synchronously from the top.

  4. it.next() is called and returns the promise object resp (which goes unused).

  5. it.next() is called again with "should go second" presented as the return value of yield in the generator function, which logs it to the console.

  6. The code thread as posted completes execution. The onFulfilled listener for promise p is scheduled for upcoming execution and logs should go first when it executes.

Question 2.

some value is currently returned from the onfulfilled listener of p , and as such would be be used to fullfill the next chained promise, created by calling. then on resp . If you want to pass it to the generator function, replace return 'some value' with it.next('some value') .

Conclusion

The bottom line is that all promises objects defined in a single piece of synchronous code are created in their entirety, with call back linkages in place, before any asynchronous processing of onFulfilled or onRejected functions supplied through then/.catch calls starts to takes place. This is true even if promises are resolved (synchronously) during their definition.

  1. promise p is yielded before console.log(second) in generator function, so why console.log(second) logged before console.log from given promise?

what you're yielding is the promise object if you check it's value you'll see the status is pending

..How could I console.log it in my gen function? Is it even possible?

in a then method you can log it

 var p = new Promise((resolve) => { resolve('should go first?'); }) var resp = p.then((first) => { console.log(first); return 'some value'; //pertain to the secondary question }) function* gen(prom){ var second = yield prom; console.log(second); prom.then((x)=> { console.log(x); }); } var it = gen(resp); var first = it.next().value; console.log(first); it.next('should go second?'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM