HTML and CSS Reference
In-Depth Information
registered. Because the remaining tests have not yet been updated to use the promise,
and because we previously decided that not handling the error was permissible, we
pass in true as the second argument to suppress this behavior. The test passes.
The next test is similar to the one we just fixed, only it verifies that leaving out the
message causes an error. Passing this test using a promise does not require further
modification of addMessage , so I will leave updating the test as an exercise.
14.4.1.3 Resolving the Promise
The next significant test to update is the one that asserts that the newly added
message object is passed to the callback. This test only requires a small change.
Because the promise has separate success and failure handlers, we can remove the
error parameter to the callback. The test can be seen in Listing 14.50.
Listing 14.50 Expecting the promise to emit success
"should call callback with new object": function (test) {
var txt = "Some message";
this.room.addMessage("cjno", txt).then(function (msg) {
test.isObject(msg);
test.isNumber(msg.id);
test.equals(msg.message, txt);
test.equals(msg.user, "cjno");
test.done();
});
}
Updating the implementation is a matter of calling the promise's resolve
method, as seen in Listing 14.51.
Listing 14.51 Resolving with the message
addMessage: function (user, message, callback) {
var promise = new Promise()
process.nextTick(function () {
/* ... */
if (!err) {
/* ... */
this.messages.push(data);
promise.resolve(data);
}
 
Search WWH ::




Custom Search