HTML and CSS Reference
In-Depth Information Returning a Promise
We will start refactoring by introducing a new test, one that expects addMessage
to return a promise object, seen in Listing 14.46.
Listing 14.46 Expecting addMessage to return a promise
testCase(exports, "chatRoom.addMessage", {
/* ... */
"should return a promise": function (test) {
var result ="cjno", "message");
Notice that I assume you've solved the exercise from before; the test case
should now be using a setup method to create a chatRoom object, available in .
The test fails as the method is currently not returning an object. We'll fix that
by returning an empty promise object, as in Listing 14.47.
Listing 14.47 Returning an empty promise object
var Promise = require("node-promise/promise").Promise;
var id = 0;
var chatRoom = {
addMessage: function (user, message, callback) {
process.nextTick(function () {
/* ... */
return new Promise();
/* ... */
Search WWH ::

Custom Search