导航
function * foo() {
yield 'Hello world';
}
const iter = foo();
console.log(iter);
function * foo() {
console.log(1);
yield 'Hello';
console.log(2);
yield 'world';
console.log(3);
return '!!!';
}
const iter = foo();
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
function * test() {
yield 'a';
yield 'b';
yield 'c';
return 'd'
}
var iter = test();
console.log(iter);
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next()); // return 返回
console.log(iter.next());
function * test() {
var a = yield 'a';
console.log('a==>>', a);
return 'd'
}
var iter = test();
console.log(iter);
console.log(iter.next());
console.log(iter.next());
function * test(){
var a = yield 'a';
console.log('a==>>', a);
return 'd'
}
var iter = test();
console.log(iter);
console.log(iter.next(10));
console.log(iter.next(20));
function * demo() {
console.log('hello' + (yield 123));
}
function * demo(){
foo(yield 'a', yield 'b');
}
function foo(a, b){
console.log(a, b);
}
var iter = demo();
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
function * demo(){
foo(yield 'a', yield 'b');
}
function foo(a, b){
console.log('foo--', a, b);
}
var iter = demo();
console.log(iter.next());
console.log(iter.next(1));
console.log(iter.next(2));
function * foo(){
yield 1;
yield 2;
yield 3;
yield 4;
return 5;
}
for(let i of foo()){
console.log(i); // 不会打印出return的5
}
function * foo(){
let v1 = yield 1;
console.log('v1-', v1);
let v2 = yield 2;
console.log('v2-', v2);
let v3 = yield 3;
console.log('v3-', v3);
let v4 = yield 4;
console.log('v4-', v4);
}
var iter = foo();
console.log(iter.next('A'));
console.log(iter.next('B'));
console.log(iter.next('C'));
console.log(iter.next('D'));
console.log(iter.next('E'));
console.log(iter.next('F'));
由于next()方法的参数表示上一个yield表达式的返回值,所以在第一次使用next()方法时,传递参数是无效的。V8 引擎直接忽略第一次使用next()方法时的参数,只有从第二次使用next()方法开始,参数才是有效的。从语义上讲,第一个next()方法用来启动遍历器对象,所以不用带有参数。
var obj = {
start: [1,2,3],
end: [7,8,9],
[Symbol.iterator]: function * (){
var nextIndex = 0,
arr = [...this.start, ...this.end];
while(nextIndex < arr.length){
yield arr[nextIndex ++]
}
}
}
for(let i of obj){
console.log(i);
}
let fs = require('fs')
function promisify(fn){
return function(...args){
return new Promise((resolve,reject)=>{
fn(...args,(err,data)=>{
if(data){
resolve(data)
}else{
reject(err)
}
})
})
}
}
let readFile = promisify(fs.readFile);
readFile('./name.txt', 'utf-8')
.then(res => readFile(res, 'utf-8'))
.then(res => readFile(res, 'utf-8'))
.then(res => console.log(res))
let fs = require('fs')
function promisify(fn){
return function(...args){
return new Promise((resolve,reject)=>{
fn(...args,(err,data)=>{
if(data){
resolve(data)
}else{
reject(err)
}
})
})
}
}
var readFile = promisify(fs.readFile);
function * read(){
let value1 = yield readFile('./name.txt', 'utf-8')
let value2 = yield readFile(value1, 'utf-8')
let value3 = yield readFile(value2, 'utf-8')
console.log(value3)
}
var iter = read();
let {value, done} = iter.next();
value.then(res => {
console.log(res)
let {value, done} = iter.next(res); // res 会赋值给value1
value.then(val => {
let {value, done} = iter.next(val); // val 会赋值给value2
value.then(val3 => {
console.log(val3);
})
})
})
let fs = require('fs')
function promisify(fn){
return function(...args){
return new Promise((resolve,reject)=>{
fn(...args,(err,data)=>{
if(data){
resolve(data)
}else{
reject(err)
}
})
})
}
}
var readFile = promisify(fs.readFile);
function * read(){
let value1 = yield readFile('./name.txt', 'utf-8')
let value2 = yield readFile(value1, 'utf-8')
let value3 = yield readFile(value2, 'utf-8')
console.log(value3)
}
function Co(iter){
return new Promise((resolve, reject)=>{
let next = (data) => {
let {value, done} = iter.next(data);
if(done){
resolve(value)
}else{
value.then((val)=>{
next(val)
})
}
}
next();
})
}
let promise = Co(read())
promise.then(val => console.log(val))
const fs = require('fs');
const promisify = (fn) => {
return function(...args) {
return new Promise((resolve, reject) => {
fn(...args, (err, data) => {
if (data) {
resolve(data)
} else {
reject(err)
}
})
});
}
}
const readFile = promisify(fs.readFile);
function * read() {
const value1 = yield readFile('./first.txt', 'utf-8');
const value2 = yield readFile(value1, 'utf-8');
const value3 = yield readFile(value2, 'utf-8');
return value3;
}
function Co(iter) {
return new Promise((resolve, reject) => {
const next = (data) => {
const { value, done } = iter.next(data);
if (done) {
resolve(value);
} else {
value.then(val => next(val))
}
}
next();
})
}
const p = Co(read());
p.then(val => console.log(val));
function * gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen();
console.log(g);
function * gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen();
console.log(g.next());
console.log(g.next());
console.log(g.return());
console.log(g.next());
console.log(g.next());
function * gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen();
console.log(g.next());
console.log(g.next());
console.log(g.return(10));
console.log(g.next());
console.log(g.next());
function * gen() {
yield 1;
yield 2;
return 10;
yield 3;
}
var g = gen();
console.log(g.next());
console.log(g.next());
console.log(g.next());
console.log(g.next());
var g = function * (){
try{
yield;
}catch(e){
console.log('生成器异常:', e)
}
console.log(1);
}
var i = g();
console.log(i.throw('a'))
var g = function * (){
try{
yield;
}catch(e){
console.log('生成器异常:', e)
}
console.log(1);
}
var i = g();
console.log(i.next())
console.log(i.throw('a'))
var g = function * (){
yield 1;
try{
yield 2;
}catch(e){
console.log('生成器异常:', e)
}
yield 3;
}
var i = g();
console.log(i.next())
console.log(i.next())
console.log(i.throw('a')) // 相当于执行了第三次next
console.log(i.next())
const fs = require('fs');
const util = require('util');
const co = require('co');
const readFile = util.promisify(fs.readFile);
function * read(){
try {
let value1 = yield readFile('./first.tx','utf-8');
let value2 = yield readFile(value1,'utf-8');
let value3 = yield readFile(value2,'utf-8');
return value3;
} catch (error) {
console.log('15'+error)
}
console.log('hello world');
}
const p = co(read());
p.then(val => console.log(val))