0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/mjsunit/test-promise-wait.js
Ryan Dahl 8185e1fd25 Remove include() add node.mixin()
include() should not be used by libraries because it will pollute the global
namespace. To discourage this behavior and bring Node more in-line with
the current CommonJS module system, include() is removed.

Small scripts like unit tests often times do want to pollute the global
namespace for ease. To avoid the boiler plate code of

  var x = require("/x.js");
  var foo = x.foo;
  var bar = x.bar;

The function node.mixin() is stolen from jQuery's jQuery.extend. So that it
can be written:

  node.mixin(require("/x.js"));

Reference:
http://docs.jquery.com/Utilities/jQuery.extend
http://groups.google.com/group/nodejs/browse_thread/thread/f9ac83e5c11e7e87
2009-10-05 15:46:31 +02:00

83 lines
1.5 KiB
JavaScript

node.mixin(require("common.js"));
var p1_done = false;
var p1 = new node.Promise();
p1.addCallback(function () {
assertEquals(1, arguments.length);
assertEquals("single arg", arguments[0]);
p1_done = true;
});
var p2_done = false;
var p2 = new node.Promise();
p2.addCallback(function () {
p2_done = true;
setTimeout(function () {
p1.emitSuccess("single arg");
}, 100);
});
var p3_done = false;
var p3 = new node.Promise();
p3.addCallback(function () {
p3_done = true;
});
var p4_done = false;
var p4 = new node.Promise();
p4.addCallback(function () {
assertEquals(3, arguments.length);
assertEquals("a", arguments[0]);
assertEquals("b", arguments[1]);
assertEquals("c", arguments[2]);
p4_done = true;
});
var p5_done = false;
var p5 = new node.Promise();
p5.addCallback(function () {
p5_done = true;
setTimeout(function () {
p4.emitSuccess("a","b","c");
}, 100);
});
p2.emitSuccess();
assertFalse(p1_done);
assertTrue(p2_done);
assertFalse(p3_done);
var ret1 = p1.wait()
assertEquals("single arg", ret1);
assertTrue(p1_done);
assertTrue(p2_done);
assertFalse(p3_done);
p3.emitSuccess();
assertFalse(p4_done);
assertFalse(p5_done);
p5.emitSuccess();
assertFalse(p4_done);
assertTrue(p5_done);
var ret4 = p4.wait();
assertArrayEquals(["a","b","c"], ret4);
assertTrue(p4_done);
process.addListener("exit", function () {
assertTrue(p1_done);
assertTrue(p2_done);
assertTrue(p3_done);
assertTrue(p4_done);
assertTrue(p5_done);
});