Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
lucas.parsy
jio
Commits
c0b1a7fd
Commit
c0b1a7fd
authored
Apr 25, 2014
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve async foreach performance
parent
2fe53871
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
18 deletions
+29
-18
src/jio/core/util.js
src/jio/core/util.js
+29
-18
No files found.
src/jio/core/util.js
View file @
c0b1a7fd
...
@@ -496,41 +496,52 @@ function methodType(method) {
...
@@ -496,41 +496,52 @@ function methodType(method) {
* @param {Any} [thisArg] Value to use as `this` when executing `callback`.
* @param {Any} [thisArg] Value to use as `this` when executing `callback`.
* @param {Promise} A new promise.
* @param {Promise} A new promise.
*/
*/
function
forEach
(
array
,
fn
,
thisArg
)
{
function
forEach
(
array
,
callback
,
thisArg
)
{
if
(
arguments
.
length
===
0
)
{
if
(
arguments
.
length
===
0
)
{
throw
new
TypeError
(
"
missing argument 0 when calling function forEach
"
);
throw
new
TypeError
(
"
forEach(): missing argument 1
"
);
}
}
if
(
!
Array
.
isArray
(
array
))
{
if
(
!
Array
.
isArray
(
array
))
{
throw
new
TypeError
(
array
+
"
is not an array
"
);
throw
new
TypeError
(
"
forEach(): argument 1
is not an array
"
);
}
}
if
(
arguments
.
length
===
1
)
{
if
(
arguments
.
length
===
1
)
{
throw
new
TypeError
(
"
missing argument 1 when calling function forEach
"
);
throw
new
TypeError
(
"
forEach(): missing argument 2
"
);
}
}
if
(
typeof
fn
!==
"
function
"
)
{
if
(
typeof
callback
!==
"
function
"
)
{
throw
new
TypeError
(
fn
+
"
is not a function
"
);
throw
new
TypeError
(
"
forEach(): argument 2
is not a function
"
);
}
}
var
cancelled
,
current_promise
=
RSVP
.
resolve
()
;
var
cancelled
,
p1
=
RSVP
.
resolve
(),
p2
;
return
new
RSVP
.
Promise
(
function
(
done
,
fail
,
notify
)
{
return
new
RSVP
.
Promise
(
function
(
done
,
fail
,
notify
)
{
var
i
=
0
;
var
i
=
0
,
value
;
function
next
()
{
function
next
()
{
if
(
cancelled
)
{
// if cancelled before `next` execution, `next` should not be called
fail
(
new
Error
(
"
Cancelled
"
));
if
(
i
<
array
.
length
)
{
try
{
value
=
callback
.
call
(
thisArg
,
array
[
i
],
i
,
array
);
}
catch
(
e
)
{
fail
(
e
);
return
;
return
;
}
}
if
(
i
<
array
.
length
)
{
// can be cancelled during callback
current_promise
=
if
(
cancelled
)
{
return
;
}
current_promise
.
then
(
fn
.
bind
(
thisArg
,
array
[
i
],
i
,
array
));
current_promise
.
then
(
next
,
fail
,
notify
);
i
+=
1
;
i
+=
1
;
if
(
value
&&
typeof
value
.
then
===
"
function
"
)
{
p1
=
value
;
p2
=
value
.
then
(
next
,
fail
,
notify
);
}
else
{
p2
=
p2
.
then
(
next
,
fail
,
notify
);
}
return
;
return
;
}
}
done
();
done
();
}
}
next
(
);
p2
=
p1
.
then
(
next
);
},
function
()
{
},
function
()
{
cancelled
=
true
;
cancelled
=
true
;
if
(
typeof
current_promise
.
cancel
===
"
function
"
)
{
if
(
typeof
p1
.
cancel
===
"
function
"
)
{
current_promise
.
cancel
();
p1
.
cancel
();
}
if
(
typeof
p2
.
cancel
===
"
function
"
)
{
p2
.
cancel
();
}
}
});
});
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment