0.10.1 • Public • Published


    A small JavaScript library for creating custom promises.


    The promise specifications in JavaScript makes it hard to subclass Promise as the specifications does not allow for constructors that takes anything else but a function as a callback.

    See the ECMA specifications for promises here


    I've created this small class that follows the specifications for what is a promise. All promises needs to have a then method that takes two parameters – catch is syntactic sugar added by the JavaScript engine.

    How to Use

    It's dead simple!

    You subclass CustomPromise and then you call either this._resolve(value) or this._reject(error) when the promise is done.

    State and callback handling is handled under the hood.

    The constructor for CustomPromise takes no parameters, so you can customize your subclasses to whatever needs, you might have. You do, though, need to call super() doing construction.


    import CustomPromise from '@trenskow/custom-promise';
    import readline from 'readline';
    class AskQuestion extends CustomPromise {
    	constructor(question) {
    		this._rl = readline.createInterface({
    			input: process.stdin,
    			terminal: true
    		this._rl.question(question, (answer) => {
    			if (answer === '!') this._reject(new Error('User refused to answer!'))
    			else this._resolve(answer);

    Now you can use it like this.

    await new AskQuestion('What is your name?');

    Another example

    The above use case for this might be a little impractical, as you might as well just implement it using regular promises or an asynchronous function.

    But a useful use case is when you want to provide promise capability on daisy chained APIs.

    import CustomPromise from '@trenskow/custom-promise';
    class DoIt extends CustomPromise {
    	constructor () {
    		setImmediate(() => {
    	take() { return this; }
    	your() { return this; }
    	mama() { return this; }
    	out() { return this; }
    	all() { return this; }
    	night() { return this; }
    	_exec() {
    		this._resolve('Baby, you\'re a full grown man!');

    This can be used like this.

    await new DoIt()
    	.night(); // Returns 'Baby, you\'re a full grown man!'

    This is not really possible with promises as they are implemented in ECMAScript, and therefore you cannot really create APIs like this – but with CustomPromise you can..! 😉


    See LICENSE.


    npm i @trenskow/custom-promise

    DownloadsWeekly Downloads






    Unpacked Size

    6.6 kB

    Total Files


    Last publish


    • trenskow