(In constant update……)

Promise

// Promise will be executed immediately after it is created
const promise = new Promise((resolve,reject) = >{
    // some code
    if(/* The operation succeeded */) {// pending->resolved
        resolve(data);
    }else {
        // pending->rejectedreject(data); }})/ / callback
promise().then(data= >{
    console.log(data)
})
Copy the code

Promise can express asynchronous operations as a flow of synchronous operations, eliminating layers of nested callback functions and providing a unified interface for Promise objects, making control easier

Disadvantages: Can't cancel a Promise halfway, once a new Promise is created, it will be executed; If no callback function is set, no exception is actively thrown. There is no way to know whether pending is in a specific state, such as just started or about to complete

Generator

function* ExampleGenerator(){
    yield 'hello~';
    yield 'king';
    return 'bye~'
}
let eg = ExampleGenerator();
eg.next();  // {value: "hello~", done: false}
eg.next();  // {value: "king", done: false}
eg.next();  // {value: "bye~", done: true}
eg.next();  // {value: undefined, done: true}
Copy the code

When the Generator is called, it is not executed and returns a pointer to the internal state (the Iterator object). Generator functions can be understood as a state machine that encapsulates multiple internal states

  • Yield expression

    • This function can only be used in Generator functions and will generate an error if used elsewhere. A Generator function can dispense with yield expressions and become a purely deferred function

    • There are similarities and differences with a return statement. The similarity is that you can return the value of the expression immediately following the statement. The difference is that the function suspends at yield and continues backward from that position the next time, whereas the return statement does not have location memory. Only one return can be executed in a function. Yield can be executed many times

  • Next () method

    • The next(params) argument represents the return value of the previous yield expression, so passing is invalid the first time next is used. Semantically, the first next method is used to start the traverser object, so no arguments are required

    • This feature has important syntactic implications. The context state of a Generator function does not change from its paused state to its resumed state. With the parameters of the next method, there is a way to continue injecting values into the function body after the Generator function has started running. That is, the behavior of the Generator function can be adjusted by injecting different values from outside to inside at different stages of its operation.

async

Async is the syntactic sugar of Generator functions. ES2017 standard introduces async functions to make asynchronous operations more convenient

const asy = async function(){
    let a = await 'hello';
    let b = await 'bye';
    console.log(a);
    console.log(b);
}
asy(); // 'hello' 'bye'
Copy the code

Different from the Generator

  • Built-in actuators. Generator functions must be executed by an executor, hence the CO module, while async functions have their own executor. In other words, async functions are executed exactly like normal functions, with only one line.

  • Better semantics. Async and await are semantic clearer than asterisks and yield. Async means that there is an asynchronous operation in a function, and await means that the following expression needs to wait for the result

  • Wider applicability. According to the CO module convention, yield can only be followed by Thunk or Promise, while async can be followed by await and Promise and primitive type values (numeric, string, Boolean, etc.). But that automatically changes to an immediate Resolved Promise)

  • The return value is Promise. Async functions return a Promise object, which is much more convenient than Generator functions returning an Iterator. You can specify what to do next using the then method. Further, async functions can be thought of as multiple asynchronous operations wrapped as a Promise object, and await commands are syntactic sugar for internal THEN commands