js promise

by John Kiran

JavaScript

'use strict';
var promiseCount = 0;

function processSync(){
alert("processSync");
var result = "request failed"; 
  if (getRandomInt(1, 10) % 2 == 0)
						result = "success";
                alert("processSync completed");
       
return result;
}

function process() {
alert("process");

	let p1 = new Promise(
		// The executor function is called with the ability to resolve or
		// reject the promise
		(resolve, reject) => {

			// This is only an example to create asynchronism
			window.setTimeout(
				function () {
					// We fulfill the promise !
          if (getRandomInt(1, 10) % 2 == 0)
						resolve("success");
           reject(" request failed"); 
           alert("process completed");
				}, Math.random() * 2000 + 1000);
		}
	);
alert("p1"+p1);
	
}

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
/* var res = process();
 */

process().then(function(res){
alert(processSync());
}, function(err){
alert(processSync());
})