Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
rsvp.js
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boris Kocherov
rsvp.js
Commits
be74db04
Commit
be74db04
authored
Sep 23, 2013
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add RSVP#any
parent
34045c37
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
210 additions
and
17 deletions
+210
-17
lib/rsvp.js
lib/rsvp.js
+2
-2
lib/rsvp/all.js
lib/rsvp/all.js
+35
-15
test/tests/extension_test.js
test/tests/extension_test.js
+173
-0
No files found.
lib/rsvp.js
View file @
be74db04
...
...
@@ -2,7 +2,7 @@ import { EventTarget } from "./rsvp/events";
import
{
CancellationError
}
from
"
./rsvp/cancellation_error
"
;
import
{
Promise
}
from
"
./rsvp/promise
"
;
import
{
denodeify
}
from
"
./rsvp/node
"
;
import
{
all
}
from
"
./rsvp/all
"
;
import
{
all
,
any
}
from
"
./rsvp/all
"
;
import
{
delay
,
timeout
}
from
"
./rsvp/timeout
"
;
import
{
hash
}
from
"
./rsvp/hash
"
;
import
{
rethrow
}
from
"
./rsvp/rethrow
"
;
...
...
@@ -15,4 +15,4 @@ function configure(name, value) {
config
[
name
]
=
value
;
}
export
{
CancellationError
,
Promise
,
EventTarget
,
all
,
delay
,
timeout
,
hash
,
rethrow
,
defer
,
denodeify
,
configure
,
resolve
,
reject
};
export
{
CancellationError
,
Promise
,
EventTarget
,
all
,
any
,
delay
,
timeout
,
hash
,
rethrow
,
defer
,
denodeify
,
configure
,
resolve
,
reject
};
lib/rsvp/all.js
View file @
be74db04
...
...
@@ -2,18 +2,33 @@
import
{
Promise
}
from
"
./promise
"
;
function
all
(
promises
)
{
function
promiseAtLeast
(
expected_count
,
promises
)
{
if
(
Object
.
prototype
.
toString
.
call
(
promises
)
!==
"
[object Array]
"
)
{
throw
new
TypeError
(
'
You must pass an array to all.
'
);
}
function
canceller
()
{
var
promise
;
for
(
var
i
=
0
;
i
<
promises
.
length
;
i
++
)
{
promise
=
promises
[
i
];
if
(
promise
&&
typeof
promise
.
then
===
'
function
'
)
{
promise
.
cancel
();
}
}
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
results
=
[],
remaining
=
promises
.
length
,
promise
;
promise
,
remaining_count
=
promises
.
length
-
expected_count
;
if
(
remaining
===
0
)
{
if
(
expected_count
===
1
)
{
resolve
();
}
else
{
resolve
([]);
}
}
function
resolver
(
index
)
{
return
function
(
value
)
{
...
...
@@ -23,8 +38,13 @@ function all(promises) {
function
resolveAll
(
index
,
value
)
{
results
[
index
]
=
value
;
if
(
--
remaining
===
0
)
{
if
(
--
remaining
===
remaining_count
)
{
if
(
remaining_count
===
0
)
{
resolve
(
results
);
}
else
{
resolve
(
value
);
canceller
();
}
}
}
...
...
@@ -37,16 +57,16 @@ function all(promises) {
resolveAll
(
i
,
promise
);
}
}
},
function
()
{
var
promise
;
for
(
var
i
=
0
;
i
<
promises
.
length
;
i
++
)
{
promise
=
promises
[
i
];
},
canceller
);
}
if
(
promise
&&
typeof
promise
.
then
===
'
function
'
)
{
promise
.
cancel
();
}
}
});
function
all
(
promises
)
{
return
promiseAtLeast
(
promises
.
length
,
promises
);
}
function
any
(
promises
)
{
return
promiseAtLeast
(
1
,
promises
);
}
export
{
all
};
export
{
all
,
any
};
test/tests/extension_test.js
View file @
be74db04
...
...
@@ -725,6 +725,179 @@ describe("RSVP extensions", function() {
});
});
describe
(
"
RSVP.any
"
,
function
()
{
it
(
'
should exist
'
,
function
()
{
assert
(
RSVP
.
any
);
});
it
(
'
throws when not passed an array
'
,
function
()
{
assert
.
throws
(
function
()
{
var
any
=
RSVP
.
any
();
},
TypeError
);
assert
.
throws
(
function
()
{
var
any
=
RSVP
.
any
(
''
);
},
TypeError
);
assert
.
throws
(
function
()
{
var
any
=
RSVP
.
any
({});
},
TypeError
);
});
specify
(
'
fulfilled only after one of the other promises are fulfilled
'
,
function
(
done
)
{
var
firstResolved
,
secondResolved
,
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
)
{
firstResolver
=
resolve
;
});
first
.
then
(
function
()
{
firstResolved
=
true
;
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
)
{
secondResolver
=
resolve
;
});
second
.
then
(
function
()
{
secondResolved
=
true
;
});
setTimeout
(
function
()
{
firstResolver
(
true
);
},
20
);
setTimeout
(
function
()
{
secondResolver
(
true
);
},
0
);
RSVP
.
any
([
first
,
second
]).
then
(
function
()
{
assert
(
firstResolved
===
undefined
);
assert
(
secondResolved
);
done
();
});
});
specify
(
'
rejected as soon as a promise is rejected
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
setTimeout
(
function
()
{
firstResolver
.
reject
({});
},
0
);
setTimeout
(
function
()
{
secondResolver
.
resolve
(
true
);
},
5000
);
RSVP
.
any
([
first
,
second
]).
then
(
function
()
{
assert
(
false
);
},
function
()
{
assert
(
first
.
isRejected
);
assert
(
!
second
.
isResolved
);
done
();
});
});
specify
(
'
passes the resolved value of the fulfilled promise to the callback
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
,
thirdResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
third
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
thirdResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
thirdResolver
.
resolve
(
3
);
firstResolver
.
resolve
(
1
);
secondResolver
.
resolve
(
2
);
RSVP
.
any
([
first
,
second
,
third
]).
then
(
function
(
result
)
{
assert
.
equal
(
result
,
3
);
done
();
});
});
specify
(
'
resolves an empty array passed to RSVP.any()
'
,
function
(
done
)
{
RSVP
.
any
([]).
then
(
function
(
result
)
{
console
.
log
(
result
);
assert
.
equal
(
result
,
undefined
);
done
();
});
});
specify
(
'
works with a mix of promises and thenables and non-promises
'
,
function
(
done
)
{
var
promise
=
new
RSVP
.
Promise
(
function
(
resolve
)
{
resolve
(
1
);
});
var
syncThenable
=
{
then
:
function
(
onFulfilled
)
{
onFulfilled
(
2
);
}
};
var
asyncThenable
=
{
then
:
function
(
onFulfilled
)
{
setTimeout
(
function
()
{
onFulfilled
(
3
);
},
0
);
}
};
var
nonPromise
=
4
;
RSVP
.
any
([
promise
,
syncThenable
,
asyncThenable
,
nonPromise
]).
then
(
function
(
result
)
{
assert
.
equal
(
result
,
2
);
done
();
});
});
specify
(
'
cancel the remaining promises as soon as resolved
'
,
function
(
done
)
{
var
promise
=
new
RSVP
.
Promise
(
function
(
resolve
)
{
resolve
(
1
);
});
var
asyncPromise
=
new
RSVP
.
Promise
(
function
(
resolve
)
{
setTimeout
(
function
()
{
resolve
(
2
);
},
0
);
});
RSVP
.
any
([
promise
,
asyncPromise
]).
then
(
function
(
result
)
{
assert
.
equal
(
result
,
1
);
});
setTimeout
(
function
()
{
assert
(
asyncPromise
.
isRejected
);
assert
(
asyncPromise
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
done
();
},
20
);
});
specify
(
'
cancel the array of promise as soon as cancelled
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
setTimeout
(
function
()
{
firstResolver
.
reject
({});
},
0
);
setTimeout
(
function
()
{
secondResolver
.
resolve
(
true
);
},
5000
);
var
all_promise
=
RSVP
.
any
([
first
,
second
]);
all_promise
.
cancel
();
setTimeout
(
function
()
{
assert
(
first
.
isRejected
);
assert
(
first
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
assert
(
second
.
isRejected
);
assert
(
second
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
done
();
},
20
);
});
});
describe
(
"
RSVP.reject
"
,
function
(){
specify
(
"
it should exist
"
,
function
(){
assert
(
RSVP
.
reject
);
...
...
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