Difference between async/await and ES6 yield with generators

前端 未结 6 777
一个人的身影
一个人的身影 2021-01-30 00:03

I was just reading this fantastic article «Generators» and it clearly highlights this function, which is a helper function for handling generator functions:



        
相关标签:
6条回答
  • 2021-01-30 00:31

    yield can be considered to be the building block of await. yield takes the value it's given and passes it to the caller. The caller can then do whatever it wishes with that value (1). Later the caller may give a value back to the generator (via generator.next()) which becomes the result of the yield expression (2), or an error that will appear to be thrown by the yield expression (3).

    async-await can be considered to use yield. At (1) the caller (i.e. the async-await driver - similar to the function you posted) will wrap the value in a promise using a similar algorithm to new Promise(r => r(value) (note, not Promise.resolve, but that's not a big deal). It then waits for the promise to resolve. If it fulfills, it passes the fulfilled value back at (2). If it rejects, it throws the rejection reason as an error at (3).

    So the utility of async-await is this machinery that uses yield to unwrap the yielded value as a promise and pass its resolved value back, repeating until the function returns its final value.

    0 讨论(0)
  • 2021-01-30 00:31

    what the heck is the difference between the await keyword and the yield keyword?

    The await keyword is only to be used in async functions, while the yield keyword is only to be used in generator function*s. And those are obviously different as well - the one returns promises, the other returns generators.

    Does await always turn something into a promise, whereas yield makes no such guarantee?

    Yes, await will call Promise.resolve on the awaited value.

    yield just yields the value outside of the generator.

    0 讨论(0)
  • 2021-01-30 00:31

    In many ways, generators are a superset of async/await. Right now async/await has cleaner stack traces than co, the most popular async/await-like generator based lib. You can implement your own flavor of async/await using generators and add new features, like built-in support for yield on non-promises or building it on RxJS observables.

    So, in short, generators give you more flexibility and generator-based libs generally have more features. But async/await is a core part of the language, it's standardized and won't change under you, and you don't need a library to use it. I have a blog post with more details on the difference between async/await and generators.

    0 讨论(0)
  • 2021-01-30 00:34

    Well, it turns out that there is a very close relationship between async/await and generators. And I believe async/await will always be built on generators. If you look at the way Babel transpiles async/await:

    Babel takes this:

    this.it('is a test', async function () {
    
        const foo = await 3;
        const bar = await new Promise(resolve => resolve('7'));
        const baz = bar * foo;
        console.log(baz);
    
    });
    

    and turns it into this

    function _asyncToGenerator(fn) {
        return function () {
            var gen = fn.apply(this, arguments);
            return new Promise(function (resolve, reject) {
                function step(key, arg) {
                    try {
                        var info = gen[key](arg);
                        var value = info.value;
                    } catch (error) {
                        reject(error);
                        return;
                    }
                    if (info.done) {
                        resolve(value);
                    } else {
                        return Promise.resolve(value).then(function (value) {
                            return step("next", value);
                        }, function (err) {
                            return step("throw", err);
                        });
                    }
                }
    
                return step("next");
            });
        };
    }
    
    
    this.it('is a test', _asyncToGenerator(function* () {   // << now it's a generator
    
        const foo = yield 3;    //  <<< now it's yield, not await
        const bar = yield new Promise(resolve => resolve(7));
        const baz = bar * foo;
        console.log(baz);
    
    }));
    

    you do the math.

    This makes it look like the async keyword is just that wrapper function, but if that's the case then await just gets turned into yield, there will probably be a bit more to the picture later on when they become native.

    You can see more of an explanation for this here: https://www.promisejs.org/generators/

    0 讨论(0)
  • 2021-01-30 00:40

    tl;dr

    Use async/await 99% of the time over generators. Why?

    1. async/await directly replaces the most common workflow of promise chains allowing code to be declared as if it was synchronous, dramatically simplifying it.

    2. Generators abstract the use case where you would call a series of async-operations that depend on each other and eventually will be in a "done" state. The most simple example would be paging through results that eventually return the last set but you would only call a page as needed, not immediately in succession.

    3. async/await is actually an abstraction built on top of generators to make working with promises easier.

    See very in-depth Explanation of Async/Await vs. Generators

    0 讨论(0)
  • 2021-01-30 00:42

    Try this test programs which I used to understand await/async with promises.

    Program #1: without promises it doesn't run in sequence

    function functionA() {
        console.log('functionA called');
        setTimeout(function() {
            console.log('functionA timeout called');
            return 10;
        }, 15000);
    
    }
    
    function functionB(valueA) {
        console.log('functionB called');
        setTimeout(function() {
            console.log('functionB timeout called = ' + valueA);
            return 20 + valueA;
        }, 10000);
    }
    
    function functionC(valueA, valueB) {
    
        console.log('functionC called');
        setTimeout(function() {
            console.log('functionC timeout called = ' + valueA);
            return valueA + valueB;
        }, 10000);
    
    }
    
    async function executeAsyncTask() {
        const valueA = await functionA();
        const valueB = await functionB(valueA);
        return functionC(valueA, valueB);
    }
    console.log('program started');
    executeAsyncTask().then(function(response) {
        console.log('response called = ' + response);
    });
    console.log('program ended');
    

    Program #2: with promises

    function functionA() {
        return new Promise((resolve, reject) => {
            console.log('functionA called');
            setTimeout(function() {
                console.log('functionA timeout called');
                // return 10;
                return resolve(10);
            }, 15000);
        });   
    }
    
    function functionB(valueA) {
        return new Promise((resolve, reject) => {
            console.log('functionB called');
            setTimeout(function() {
                console.log('functionB timeout called = ' + valueA);
                return resolve(20 + valueA);
            }, 10000);
    
        });
    }
    
    function functionC(valueA, valueB) {
        return new Promise((resolve, reject) => {
            console.log('functionC called');
            setTimeout(function() {
                console.log('functionC timeout called = ' + valueA);
                return resolve(valueA + valueB);
            }, 10000);
    
        });
    }
    
    async function executeAsyncTask() {
        const valueA = await functionA();
        const valueB = await functionB(valueA);
        return functionC(valueA, valueB);
    }
    console.log('program started');
    executeAsyncTask().then(function(response) {
        console.log('response called = ' + response);
    });
    console.log('program ended');
    
    0 讨论(0)
提交回复
热议问题