• So first of all, what are the common ways to convert an array to an array? Such as:
1 Array.prototype.slice.call(arrayLike);
2 Array.from(arrayLike);
3 […arrayLike];
  • One of the questions I’m going to talk about here is, what about class arrays?
  • In general, an array can be considered as having key, value, and length, for example:
letObj = {1-0, 1:2, length: 2};Copy the code
  • With the above three methods, the third party will report the error
letarr = [...obj]; //obj is not iterableCopy the code
  • An iterator is missing

1.generator

let obj = {
    0:'1',
    1:'2',
    length:2,
    [Symbol.iterator]:function() {// iteratorlet index = 0;
        letThat = this// This in the iterator is the objectreturn{// Returns an objectnext() {
        		return{value: that[index],//value stands for valuedone: (that.length === index++)?true:false//doneRepresents whether the iteration is complete,trueIteration completion}}}}}Copy the code
  • The next method in the iterator is called directly with […obj]. A next method with value and done, each time next is called, execute… And the generator function can do that for us automatically.
function *gen(x) {
    letb = yield x; // * Use with yieldlet c= yield b;
    returnC}// Each time yield is paused. console.log(gen('Jj Lin').next())// Yield x and first pass x console.log(gen()'wang jiao'Next ())// The next time next is called, the argument is assigned to b, and the next time next is passed, the yield value is returnedCopy the code
function *a() {
    yield '1';
    yield '2';
}
function *b(){
    yield '3'; yield *a(); // To nest in generator, again add × yield'4';
}
Copy the code
  • If the example above is combined with promises, when promises are nested in multiple layers
let {promisify} = require('bulebird'); // Make asynchronous methods promiselet fs =require('fs');
let read = promisify(fs.read);
function *gen() {// read a. TXT first, then assign the contents to b, continue readinglet b = yield read('a.txt'.'utf8');
    let c = yield read(b,'utf8');
    return c; 
}
letit = gen(); / / iterator it. Next () value. Then ((data) = > {/ / it. The next (). The yield valueread('a.txt'), get the promise, continuethenNext (data).value. Then ((data)=>{it. Next (data); })})Copy the code

2.co

  • Co can be used to execute multiple nested promises, requiring each yield to be followed by a promise(CO is a node package).
let co = require('co');
co(gen()).then(data=>{
    console.log(data);
})
Copy the code
  • Below is handwritten CO
function co(it){
    return new Promise((resolve, reject)=>{
        function next() {// async recursionlet {value, done} = it.next(); // Return two values,if(!done{/ / according todoneTo see if we're therereturnvalue.then((data)=>{ next(data); Next},reject) when the first promise is finished. // If one promise fails, it fails.else{// Return resolve(value); } } next(); })}Copy the code

3. Async, await are syntactic sugar of generator, equivalent to generator+co

async function r(){// Do not write generatorlet b = await read('a.txt,'utf8);
    let c = await read(b, 'utf8');
    returnc; } r().then(data=>{ console.log(data); }) console.log(r); // All async declared functions are promisesCopy the code