Right promis, cant?

There is a task: to get some data from the page to generate URLs with these data, and then send the requests to the URLs in response to check.
Compiled code like this:
function checkSpec() {
 return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
 "$.each(xmlDataSpeclist, function (key, item) {" +
 "search.push(" +
 "'http://domain/?spec='" + " + item.id" +
 ");" +
 "});" +
 "};" +
 "return search;"
 ).then((search) => {
 return search.forEach(function(val, i) {
 console.log(search.length); //debug printing
 return new Promise((resolve, reject) => {
 request(search[i], function(error, response, body){
 if (error) {
reject(error);
}
resolve(body);
});
 }).then((body) => {
console.log(body);
 assert.include(body, 'class="someClass"');
});
});
});

}

 return checkSpec();

But it works wrong. First comes the enumeration of the array, step test, allegedly goes well, begin the steppes, and somewhere in the middle starts to receive responses (output to the console body).
Where you need to improve the promis that it worked fine? I.e. each element of the array - enquiry - check, and then the next array element, the query-check, etc.

UPD.
By changing the code in accordance with the recommendations received is:
function checkSpec() {
 return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
 "$.each(xmlDataSpeclist, function (key, item) {" +
 "search.push(" +
 "'http://domain/?spec='" + " + item.id" +
 ");" +
 "});" +
 "};" +
 "return search;"
 ).then((search) => {
 return Promise.all(search.map(function(val, i) {
/*console.log(search.length);*/
 return new Promise((resolve, reject) => {
 request(search[i], function(error, response, body){
 if (error) {
reject(error);
}
resolve(body);
});
 }).then((body) => {
 var $ = cheerio.load(body);
 var txt = $('locator1').text().replace(/\s+/g," ").trim();
 var doc = $(locator2').text().trim();
/*console.log(txt);*/
 return assert.notEqual(txt, ", "not found: "+ doc);
});
}));
});

}

 return checkSpec();

Now he at least checks everything is fine and felitsa step, when the check fails. The problem is that when a check falls - the remainder of the tests still pass through (if not to remove the console output of the text - it is clear that after the fall continues to output the text of the following tests). How to make so that after fail check to stop the other checks? Tried to insert the if (condition) {return}, but does not help, maybe there is also not inserted.
July 2nd 19 at 13:55
3 answers
July 2nd 19 at 13:57
You need to wrap the return in the cycle of promises in an array and at the end of the return Promise.all(array)

This action starts the waiting for the fulfillment of all promises in the array. Also returns a promise, which you can call
.then(...)
.catch(...)

In General, I propose to draw attention to the new async/await - with them life becomes much easier.
async/await is already working native? - arnold2 commented on July 2nd 19 at 14:00
: never thought of that)
In js yet, but I'm writing on ts - it is already built. - lavon.Turn commented on July 2nd 19 at 14:03
July 2nd 19 at 13:59
forEach always returns undefined, we need a map
To wait for the fulfillment of all the promises in the Promise array use.all
total:
function checkSpec() {
 return driver.executeScript("var search = []; if (typeof xmlDataSpeclist !== 'undefined') {" +
 "$.each(xmlDataSpeclist, function (key, item) {" +
 "search.push(" +
 "'http://domain/?spec='" + " + item.id" +
 ");" +
 "});" +
 "};" +
 "return search;"
 ).then((search) => {
 return Promise.all(search.map(function(val, i) {
 console.log(search.length); //debug printing
 return new Promise((resolve, reject) => {
 request(search[i], function(error, response, body){
 if (error) {
reject(error);
}
resolve(body);
});
 }).then((body) => {
console.log(body);
 assert.include(body, 'class="someClass"');
});
}));
});

}

 return checkSpec();
Dmitry, I added the starting post, there was nuances. Look, pliz. - arnold2 commented on July 2nd 19 at 14:02
July 2nd 19 at 14:01
var executeSequence = function(promiseFn, values, acc) {
 if (!values.length) return Promise.resolve(acc);
 var promise = promiseFn(values.shift());
 acc = acc || [];
 return promise.then(function(result) { 
acc.push(result);
 return executeSequence(promiseFn, values, acc);
});
};

var requestUrl = function(url) {
return new Promise((resolve, reject) => {
 request(url, function(error, response, body){
 if (error) {
reject(error);
}
resolve(body);
}); 
}

executeSequence(requestUrl, ['http://google.com', ...])
 .then(function(response) { 
 // response is an array of the body for each of the url in order
})
 .catch(function(reason) {

 });


https://github.com/joliss/promise-map-series

Find more questions by tags JavaScriptNode.jsSelenium