9999.99.99 • Published 4 years ago

reinforced_promise v9999.99.99

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
4 years ago

ReinforcedPromise

Reinforced Promise for Node.js.

Features

  • Turning async function into sync
  • Querying status, result, reason in script
  • Providing sleep(s), msleep(ms) functions

Usage

let ReinforcedPromise = require("reinforced_promise"), p;
// ReinforcedPromise.applyGlobalReinforcedPromise(); - Replace Promise with reinforced Promise
// ReinforcedPromise.restoreOriginalPromise();       - Restore original Promise
// ReinforcedPromise.reinforcedPromise               - Reinforced Promise

function asyncFunction(a, b, callback) {
	setTimeout(callback.bind(undefined, a + b), 3000);
}

// This will print resolved 3 after three seconds. -------------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
});
p.attach(); // 0 OK.
console.log(p.status, p.result);
// This will print pending undefined after two seconds. ---------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
});
p.attach(2000); // -3 Timeout was reached.
console.log(p.status, p.result);
// This will print pending undefined after two seconds. ---------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
	setTimeout(function () {
		detach();
	}, 2000);
});
p.attach(); // -4 Detached.
console.log(p.status, p.result);
// This will print resolved 3 after three seconds. --------------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
	setTimeout(function () {
		detach();
	}, 2000);
});
p.attach(); // -4 Detached.
p.attach(); // 0 OK.
console.log(p.status, p.result);
// This will print pending undefined after two seconds. ---------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
	setTimeout(function () {
		attachable(false);
		detach();
	}, 2000);
});
p.attach(); // -4 Detached.
p.attach(); // -1 Not attachable.
console.log(p.status, p.result);
// This will print resolved 3 after three seconds. --------------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
	setTimeout(function () {
		attachable(false);
		detach();
	}, 2000);
});
p.attach(); // -4 Detached.
p.attachable = true;
p.attach(); // 0 OK.
console.log(p.status, p.result);
// This will print -2 after three seconds. ----------------------------------------------------------
p = new ReinforcedPromise.reinforcedPromise(function (resolve, reject, attachable, detach) {
	asyncFunction(1, 2, function (sum) {
		resolve(sum);
	});
});
p.then(function () {
	console.log(p.attach()); // -2 Not pending.
});
p.attach();
// This will print 3 after three seconds. -----------------------------------------------------------
sleep(1.5);
msleep(1500);
console.log(1 + 2);
9999.99.99

4 years ago

2019.10.15

5 years ago

2019.10.14

5 years ago

2019.10.1-3.3

5 years ago

2019.10.1-3.2

5 years ago

2019.10.1-3.1

5 years ago

2019.10.13

5 years ago

2019.10.12

5 years ago