Using native javascript promises in postman

Hi,

Javascript and node now has native support for Promises. I was hoping we could use these in Postman and newman as well to chain async sendRequest calls.

Although declaring and using new Promise works but chaining does not work.

For example look at the code below (put this in test script of any request)

var promiseMethod = function(text) {
   var promise = new Promise(function(resolve, reject){
      console.log('Enter: ' + text);
      setTimeout(function() {
         console.log('Complete: ' + text);
         resolve({data: text + ' 123'});
      }, 2000);
   });
   return promise;
};

promiseMethod('first')
   .then((v) => {return promiseMethod('second');})
   .then((v) => {return promiseMethod('third');})

The Postman Console output for this is:

Enter: first
Complete: first
Enter: second

Whereas if run through NODE or even Chrome console, the output is:

Enter: first
Complete: first
Enter: second
Complete: second
Enter: third
Complete: third

Whats the reason, that only the 1st Promise call is handled by postman/newman but chained calls do not work ?

1 Like

Hi @aksbenz This is a known issue - we’re working to get this working correctly. As a temporary workaround, you can use this:

 var interval = setTimeout(function() {}, 10000),
  promiseMethod = function(text) {
   var promise = new Promise(function(resolve, reject){
      console.log('Enter: ' + text);
      setTimeout(function() {
         console.log('Complete: ' + text);
         resolve({data: text + ' 123'});
      }, 2000);
   });
   return promise;
};

promiseMethod('first')
   .then((v) => {return promiseMethod('second');})
   .then((v) => {return promiseMethod('third');})
   .then((v) => {clearTimeout(interval);})

This essentially adds a timeout that’s closed when all promises are resolved. This makes the Postman script module to wait till that time before proceeding to send the request.

Hi @abhijit do you have any update on this? are promises working properly with the latest version of postman?

We are evaluating Postman as a platform to run our E2E tests on and not having promises seriously impacts our ability to write non-trivial scripts.

Currently, I need to do 3 API calls to setup the application to be ready for testing. With callbacks this is seriously hell.

1 Like

I need to use the result of the promise as an environment variable - but it seems it’s getting set too late. Any suggestions?

"use strict";
var lock = setTimeout(function() {}, 3000);
var secret = '<redacted>';

postman.setEnvironmentVariable("nonce", Math.ceil(new Date().getTime() / 1000));

hash('SHA-256', secret + '' + postman.getEnvironmentVariable('nonce')).then(function(sha) {
    postman.setEnvironmentVariable("nonce_token", sha);
    clearTimeout(lock);
});


console.log(hash('SHA-256', secret + '' + postman.getEnvironmentVariable('nonce')));

async function hash(digest, inputString) {
	return Array.from(new Uint8Array(crypto.subtle.digest(digest, new TextEncoder("utf-8").encode(inputString)))).map(b => (b.toString(16).padStart(2, "0"))).join('');
}

Edit:
Worked around the issue using

require("crypto-js");
CryptoJS.SHA256(secret + '' + postman.getEnvironmentVariable('nonce')).toString(CryptoJS.enc.Hex);

Hey guys, thanks for the tips here. I had the same issue and just documented the solution (using a global setTimeout) at Stack Overflow.