Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
lylezhan
async_demo
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
async_demo
/
iterator.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
61 lines (55 loc) · 1.48 KB
master
Breadcrumbs
async_demo
/
iterator.js
Copy path
Top
File metadata and controls
Code
Blame
61 lines (55 loc) · 1.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var async = require('async');
var t = require('./t');
var log = t.log;
/**
* 将一组函数包装成为一个iterator,初次调用此iterator时,会执行定义中的第一个函数并返回第二个函数以供调用。
* 也可通过手动调用 next() 得到以下一个函数为起点的新的iterator。
* 该函数通常由async在内部使用,但如果需要时,也可在我们的代码中使用它。
*/
// async.iterator(tasks)
var iter = async.iterator([
function () {log('I am 111')},
function () {log('I am 222')},
function () {log('I am 333')}
]);
/**
* 直接调用(),会执行当前函数,并返回一个由下个函数为起点的新的iterator
*/
//1.1
log('1.1 iter()');
var it1 = iter();
it1();
it1();
//28.368> 1.1 iter()
//28.371> I am 111
//28.372> I am 222
//28.372> I am 222
/**
* 通过iter()来调用下一个函数
*/
log('1.2 iter()');
var it2 = iter();
var it3 = it2();
var it4 = it3();
//it4(); // 这句代码执行会报错
log(it4); // => 'null'
//32.449> 1.2 iter()
//32.452> I am 111
//32.452> I am 222
//32.453> I am 333
//32.454> null
/**
* 调用next(),不会执行当前函数,直接返回由下个函数为起点的新iterator
* 对于同一个iterator,多次调用next(),不会影响自己
*/
//1.3
log('1.3 iter()');
var it5 = iter.next();
it5();
var it6 = iter.next().next();
it6();
iter();
//39.895> 1.3 iter()
//39.898> I am 222
//39.899> I am 333
//39.899> I am 111
You can’t perform that action at this time.