1.0.1 • Public • Published


this-promise is a module that creates the Promise chain of [Array] list of promises with its own inner userContext this context.


npm install this-promise

const thisPromise = require('this-promise');


  • The this-promise function allows you to create the chain of promises by passing all promises as the [Array] list through the module function, what may turn out to be more elegant and readable in some cases.
  • Under the hood, the native Promise object with its then and catch methods is used.
  • It creates dynamically the chain of then() and catch() methods. The promises are called back-to-back, as if they were called manually with Promise.resolve().then(a).then(b).then(c).catch(err) etc.
  • It is similar to Promise.all and Promise.race built-in methods, where the iterable object is passed through it as an argument with the promises (or other values).
  • Each then and catch handler is binded to the inner userContext this object. It allows all promises to get and set the data using this keyword, rather than pass the data through resolve(data) and reject(data) callback functions to the further promises.



list [Array]
  • The [Array] list should contain the items that will be chained with Promise then method.
  • The items should be of [Function] type. They are automatically passed as Promise then(item)'s handlers
  • The [Function] item can return the [Promise] object. Then the item will be queued in the chain and the further items will be suspended till the fulfilment or rejection of this item. [see below]
  • The items can be also of any type. The value is automatically encapsulated (returned) in the function and passed as resolved Promise (send to the further promise) [see below]
finalThen [Function] optional
  • The [Function] finalThen is automatically attached at the end of the chain as then() function and executed as the last promise (if all previous promises succeeded) [see below]
finalCatch [Function] optional
  • The [Function] finalCatch is automatically attached at the end of the chain as catch() function and executed in case of the rejection of any of the promises. [see below]


The thisPromise function returns the [Promise] object, so further thens and catches can be attached to it [see below]


Mind how this keyword is used in the handlers.
Mind how promises are passed as [Array] items.
const thisPromise = require('this-promise');
function init(get){
  //set some initial data to the this userContext object
  //that will be accessible in each chained promise
  this.isCoder = true;
  this.isHuman = true;
  this.experience = 5;
function name(){
  return new Promise((resolve,reject)=>{
      //fetch async data and set it to this userContext object
      console.log('name fetched'); = 'John Doe';
function age(){
  return new Promise((resolve,reject)=>{
      //fetch another async data and set it to this userContext object
      console.log('age fetched');
      this.age = 25;
function print(){
  console.log(`I'm ${} and I'm ${this.age} years old.`);
  if(this.isHuman) console.log(`I'm also a human.`);
  if(this.isCoder) console.log(`I have ${this.experience} years’ experience as a coder.`);
function handleError(err){
The finalThen and finalCatch parameters are optional.
They are attached to the chain only if they are of [Function] type.
const thisPromise = require('this-promise');
function promise(){
  return new Promise((resolve,reject)=>{
    reject('Could not fetch the data.');
function handleError(msg){
  console.error(new Error(msg));
The [Array] item can be of any type
It is automatically encapsulated (returned) in the function
const thisPromise = require('this-promise');
thisPromise([{name:'John Doe',age:25},promise],print,handleError);
//the {name:'John Doe',age:25} is automatically encapsupated in the function
//function(){ return {name:'John Doe',age:25}; }
//and passed through then() method
function promise(getData){
  //now the {name:'John Doe',age:25} object is accessible as getData parameter =;
  this.age = getData.age;
function print(){
  console.log(`Hello, I'm ${} and I'm ${this.age} years old.`);
  //Hello, I'm John Doe and I'm 25 years old.
function handleError(msg){
  console.error(new Error(msg));
The contextPromise module returns the Promise, so the further thens and catches can be attached
Unfortunately the userContext this object is inaccessible in outer thens and catches
const thisPromise = require('this-promise');
  //this promise is out of the module function
  //so it is not binded to the userContext this object
  console.log(;  //undefined
  console.log(this.age);  //undefined
  //but the userContext this object can be passed in the resolve() callback
  console.log(; //John Doe
  console.log(getThis.age); //25
function promise(getData){
  return new Promise((resolve,reject)=>{
    setTimeout(()=>{ = 'John Done';
      this.age = 25;
      console.log('Promise resolved!');
      //as the userContext this object will not be accessible in the outer thens and catches
      //pass the userContext this object through resolve() callback function



Package Sidebar


npm i this-promise

Weekly Downloads






Unpacked Size

9.5 kB

Total Files


Last publish


  • devrafalko