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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
rsvp.js
Commits
543306a7
Commit
543306a7
authored
Sep 23, 2013
by
Romain Courteaud
🐙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add RSVP#delay, promise#delay, RSVP#timeout
parent
c2973910
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
1 deletion
+149
-1
lib/rsvp.js
lib/rsvp.js
+2
-1
lib/rsvp/timeout.js
lib/rsvp/timeout.js
+36
-0
test/tests/extension_test.js
test/tests/extension_test.js
+111
-0
No files found.
lib/rsvp.js
View file @
543306a7
...
...
@@ -3,6 +3,7 @@ import { CancellationError } from "./rsvp/cancellation_error";
import
{
Promise
}
from
"
./rsvp/promise
"
;
import
{
denodeify
}
from
"
./rsvp/node
"
;
import
{
all
}
from
"
./rsvp/all
"
;
import
{
delay
,
timeout
}
from
"
./rsvp/timeout
"
;
import
{
hash
}
from
"
./rsvp/hash
"
;
import
{
rethrow
}
from
"
./rsvp/rethrow
"
;
import
{
defer
}
from
"
./rsvp/defer
"
;
...
...
@@ -14,4 +15,4 @@ function configure(name, value) {
config
[
name
]
=
value
;
}
export
{
CancellationError
,
Promise
,
EventTarget
,
all
,
hash
,
rethrow
,
defer
,
denodeify
,
configure
,
resolve
,
reject
};
export
{
CancellationError
,
Promise
,
EventTarget
,
all
,
delay
,
timeout
,
hash
,
rethrow
,
defer
,
denodeify
,
configure
,
resolve
,
reject
};
lib/rsvp/timeout.js
0 → 100644
View file @
543306a7
import
{
Promise
}
from
"
./promise
"
;
function
promiseSetTimeout
(
millisecond
,
should_reject
,
message
)
{
var
timeout_id
;
function
resolver
(
resolve
,
reject
)
{
timeout_id
=
setTimeout
(
function
()
{
if
(
should_reject
)
{
reject
(
message
);
}
else
{
resolve
(
message
);
}
},
millisecond
);
}
function
canceller
()
{
clearTimeout
(
timeout_id
);
}
return
new
Promise
(
resolver
,
canceller
);
}
function
delay
(
millisecond
,
message
)
{
return
promiseSetTimeout
(
millisecond
,
false
,
message
);
}
function
timeout
(
millisecond
)
{
return
promiseSetTimeout
(
millisecond
,
true
,
"
Timed out after
"
+
millisecond
+
"
ms
"
);
}
Promise
.
prototype
.
delay
=
function
(
millisecond
)
{
return
this
.
then
(
function
(
fulfillmentValue
)
{
return
delay
(
millisecond
,
fulfillmentValue
);
});
};
export
{
delay
,
timeout
};
test/tests/extension_test.js
View file @
543306a7
...
...
@@ -1389,3 +1389,114 @@ describe("`always` on a promise", function () {
},
20
);
});
});
describe
(
"
`RSVP.delay`
"
,
function
()
{
it
(
'
should be a function
'
,
function
()
{
assert
.
equal
(
typeof
RSVP
.
delay
,
"
function
"
);
});
it
(
'
should return a new promise
'
,
function
()
{
var
promise
=
RSVP
.
delay
();
assert
(
promise
instanceof
RSVP
.
Promise
);
});
it
(
'
should fulfill the new promise after some milliseconds
'
,
function
(
done
)
{
var
promise
=
RSVP
.
delay
(
30
);
setTimeout
(
function
()
{
assert
.
equal
(
promise
.
isFulfilled
,
undefined
);
assert
.
equal
(
promise
.
isRejected
,
undefined
);
setTimeout
(
function
()
{
assert
.
equal
(
promise
.
isFulfilled
,
true
);
assert
.
equal
(
promise
.
isRejected
,
undefined
);
done
();
},
20
);
},
20
);
});
it
(
'
should allow to cancel the setTimeout
'
,
function
(
done
)
{
var
promise
=
RSVP
.
delay
();
promise
.
cancel
();
setTimeout
(
function
()
{
// XXX How to check that clearTimeout is called?
assert
.
equal
(
promise
.
isRejected
,
true
);
done
();
},
20
);
});
});
describe
(
"
`promise.delay`
"
,
function
()
{
it
(
'
should be a function
'
,
function
()
{
var
promise
=
new
RSVP
.
Promise
(
function
(
accept
,
fail
)
{});
assert
.
equal
(
typeof
promise
.
delay
,
"
function
"
);
});
it
(
'
should return a new promise
'
,
function
()
{
var
promise
=
new
RSVP
.
Promise
(
function
(
accept
,
fail
)
{}),
promise2
=
promise
.
delay
();
assert
(
promise2
instanceof
RSVP
.
Promise
);
});
it
(
'
should fulfill the new promise after some milliseconds
'
,
function
(
done
)
{
var
promise
=
new
RSVP
.
Promise
(
function
(
accept
,
fail
)
{
accept
();}),
promise2
=
promise
.
delay
(
30
);
setTimeout
(
function
()
{
assert
.
equal
(
promise2
.
isFulfilled
,
undefined
);
assert
.
equal
(
promise2
.
isRejected
,
undefined
);
setTimeout
(
function
()
{
assert
.
equal
(
promise2
.
isFulfilled
,
true
);
assert
.
equal
(
promise2
.
isRejected
,
undefined
);
done
();
},
20
);
},
20
);
});
it
(
'
should propagate the fulfillmentValue
'
,
function
(
done
)
{
var
promise
=
new
RSVP
.
Promise
(
function
(
accept
,
fail
)
{
accept
(
"
foo
"
);}),
promise2
=
promise
.
delay
(
30
);
setTimeout
(
function
()
{
assert
.
equal
(
promise2
.
isFulfilled
,
true
);
assert
.
equal
(
promise2
.
isRejected
,
undefined
);
assert
.
equal
(
promise2
.
fulfillmentValue
,
"
foo
"
);
done
();
},
40
);
});
});
describe
(
"
`RSVP.timeout`
"
,
function
()
{
it
(
'
should be a function
'
,
function
()
{
assert
.
equal
(
typeof
RSVP
.
timeout
,
"
function
"
);
});
it
(
'
should return a new promise
'
,
function
()
{
var
promise
=
RSVP
.
timeout
();
assert
(
promise
instanceof
RSVP
.
Promise
);
});
it
(
'
should reject the new promise after some milliseconds
'
,
function
(
done
)
{
var
promise
=
RSVP
.
timeout
(
30
);
setTimeout
(
function
()
{
assert
.
equal
(
promise
.
isFulfilled
,
undefined
);
assert
.
equal
(
promise
.
isRejected
,
undefined
);
setTimeout
(
function
()
{
assert
.
equal
(
promise
.
isFulfilled
,
undefined
);
assert
.
equal
(
promise
.
isRejected
,
true
);
assert
.
equal
(
promise
.
rejectedReason
,
"
Timed out after 30 ms
"
);
done
();
},
20
);
},
20
);
});
it
(
'
should allow to cancel the setTimeout
'
,
function
(
done
)
{
var
promise
=
RSVP
.
timeout
();
promise
.
cancel
();
setTimeout
(
function
()
{
// XXX How to check that clearTimeout is called?
assert
.
equal
(
promise
.
isRejected
,
true
);
done
();
},
20
);
});
});
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