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);
});
複製代碼