Debian Codegraph @

deferred/examples/test-with-mocha-and-should.js

raw
1 /* eslint-env mocha */
2
3 "use strict";
4
5 // Example demonstrating the use of deferred in the context of unit tests with
6 // the mocha and should modules.
7 //
8 // Usage:
9 // $ mocha test/deferred_with_mocha_and_should.test.js
10
11 var should = require("should")
12 , deferred = require("deferred");
13
14 describe("Async functions with promises", function () {
15 it("should produce a result without error", function (done) {
16 var asyncFunc1, asyncFunc2, asyncFunc3;
17
18 asyncFunc1 = function (params) {
19 console.log("params:", params);
20 var def = deferred();
21
22 // Some async processing
23 setTimeout(function () { def.resolve(params + "with"); }, 100);
24
25 return def.promise;
26 };
27
28 asyncFunc2 = function (params) {
29 console.log("params:", params);
30 var def = deferred();
31
32 // Some async processing
33 setTimeout(function () { def.resolve(params + "some"); }, 100);
34
35 return def.promise;
36 };
37
38 asyncFunc3 = function (params) {
39 console.log("params:", params);
40 var def = deferred();
41
42 // Some async processing
43 setTimeout(function () { def.resolve(params + "more"); }, 100);
44
45 return def.promise;
46 };
47
48 asyncFunc1("Intial_value").then(asyncFunc2).then(asyncFunc3).done(
49 function (result) {
50 console.log("Final result is:", result);
51 result.should.equal("Intial_valuewithsomemore");
52 done();
53 },
54 function (err) {
55 should.not.exist(err);
56 done();
57 }
58 );
59 });
60 });