How to use the events.Promise function in events

To help you get started, we’ve selected a few events examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b2--;
});

// Test late errback binding
var c = new Promise();
c.emitError(TEST_VALUE);
assert.ok(c.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.c1--;
}));

// Test errback exceptions
var d = new Promise();
d.emitError(TEST_VALUE);

process.addListener('uncaughtException', function(e) {
  if (e.name === "AssertionError") {
    throw e;
  }

  expectedCallbacks.d1--;
  assert.ok(e.message.match(/unhandled emitError/i));
});

process.addListener('exit', function() {
  for (var name in expectedCallbacks) {
    var count = expectedCallbacks[name];

    assert.equal(
github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
});
a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'normal');
});
a.emitSuccess(TEST_VALUE);

assert.ok(a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a2--;
}));
assert.ok(a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'late');
}));

// Test regular & late errback binding
var b = new Promise();
b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b1--;
});
b.emitError(TEST_VALUE);

b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b2--;
});

// Test late errback binding
var c = new Promise();
c.emitError(TEST_VALUE);
assert.ok(c.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
var
  Promise = require('events').Promise,

  TEST_VALUE = {some: 'object'},

  expectedCallbacks = {
    a1: 1,
    a2: 1,
    b1: 1,
    b2: 1,
    c1: 1,
    d1: 1,
  };

// Test regular & late callback binding
var a = new Promise();
a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a1--;
});
a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'normal');
});
a.emitSuccess(TEST_VALUE);

assert.ok(a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a2--;
}));
assert.ok(a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'late');
}));
github graalvm / graaljs / test / mjsunit / test-promise-timeout.js View on Github external
promise.addCallback(function() {
  assert.ok(false, 'addCallback should not fire after a promise error');
});

promise.addErrback(function(e) {
  assert.equal(true, e instanceof Error);
  assert.equal('timeout', e.message);
  timeouts++;
});

setTimeout(function() {
  promise.emitSuccess('Am I too late?');
}, 500);

var successPromise = new events.Promise();
successPromise.timeout(500);
setTimeout(function() {
  successPromise.emitSuccess();
}, 250);

successPromise.addErrback(function() {
  assert.ok(false, 'addErrback should not fire if there is no timeout');
});

var errorPromise = new events.Promise();
errorPromise.timeout(500);
setTimeout(function() {
  errorPromise.emitError(new Error('intentional'));
}, 250);

errorPromise.addErrback(function(e) {
github graalvm / graaljs / test / mjsunit / test-wait-ordering.js View on Github external
function timer (t) {
  var promise = new events.Promise();
  setTimeout(function () {
    promise.emitSuccess();
  }, t);
  return promise;
}
github graalvm / graaljs / test / mjsunit / test-promise-cancel.js View on Github external
process.mixin(require('./common'));
events = require('events');

var promise = new events.Promise();
var cancelled = false;
promise.addCancelback(function(){
  if(cancelled){
    assert.ok(false, "promise should not cancel more than once");
  }
  cancelled = true;
});
promise.cancel();
promise.cancel();
github mikeal / relaximation / common / os.js View on Github external
var mkdir = function (dir, mode) {
  if (!mode) {
    mode = 0755;
  }
  p = new events.Promise();
  fs.mkdir(dir, mode)
    .addCallback(function(){p.emitSuccess()})
    .addErrback(function(){throw 'failed to create '+dir});
  p.wait();
}
github mikeal / relaximation / common / os.js View on Github external
var open = function (file, mode, perm) {
  p = new events.Promise();
  if (typeof(mode) == "string") {
    mode = {w:process.O_WRONLY, r:process.O_RDONLY, c:process.O_CREAT}[mode];
  }
  if (!perm) { perm = 0755 }
  fs.open(file, mode, perm)
    .addCallback(function (fd){p.fd = fd; p.emitSuccess(fd)});
  p.wait();
  return p.fd;
}
github mikeal / relaximation / common / os.js View on Github external
if (cwd) { var olddir = process.cwd(); process.chdir(cwd);}
  var child = process.createChildProcess(cmd, args, env);
  if (cwd) { process.chdir(olddir);}
  child.addListener("output", function (data) {
    if (data) { 
      if (printBuffer) { sys.print(data);}
      buffer += data; 
    }
  });
  child.addListener("error", function(data) {
    if (data) {
      buffer += data;
      sys.print(data);
    }
  });
  var p = new events.Promise();
  child.addListener("exit", function(code) {p.code = code;p.emitSuccess();});
  p.wait();
  return [p.code, buffer];
};