let syml = Symbol('aaa');
typeof(syml) //symbol
复制代码
//定义
function * gen(){
yield 'welcome';
yield 'to';
return 'China';
}
//手动调用 next()顺序执行
let g1 = gen();
console.log(g1.next()); // {value:'welcome', done: false}
console.log(g1.next()); // {value:'to', done: false}
console.log(g1.next()); // {value:'China', done: ture}
//用 for of 循环
for(let val of g1){
console.log(val); //welcome to; return 的东西不会遍历
}
复制代码
let [a, b] = gen(); //a, b = welcome to
function * gen(){
let val = yield 'murphy';
yield axios.get('https://api..../${val}') //val为传参
}
let gi = gen();
let username = g1.next().value;
g1.next(username).value.then(res=>{
console.log(res.data);
});
复制代码