Skip to content

Latest commit

 

History

History
65 lines (53 loc) · 2.04 KB

promisify.md

File metadata and controls

65 lines (53 loc) · 2.04 KB

PoC: Design Elegant Asyschronous Functions In Node.js

On designing asynchronous functions, I used to make them return a promise OR accept a callback function. I call this PoC.

I use package undertake to create a PoC asynchronous function:

const undertake = require('undertake');

const add = undertake.async(function*(a, b) {
	// A promisified function.
	let fa = function(a) {
		return Promise.resolve(a);
	};

	//  A traditional asynchronous function.
	let fb = function(b, callback) {
		// Here `null` means no exception happens.
		callback(null, b);
	};

	let m = yield fa(a);
	let n = yield fb(b);
	let ret = m + n;
	return ret;
});

// If no callback offered, a promise will be returned.
add(1, 2)
	.then(data => {
		// ...
	}).catch(ex => {
		// ...
	});

// If callback found, it will be run.
add(1, 2, (ex, data) => {
	if (ex) {
		// ...
	}
	else {
		// ...
	}
});

Charged upon notorious Callback Hell, tranditional asynchrous functions accepting arguments list ended with a callback function are not welcome. Now in 2019, Promise and async / await become popular. Since Node.js version 8.0.0, offical method util.promisify(original) is released to help transforming a traditional asynchronous function to a promisified one.

References

版权声明 | LICENSE