Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Promises and Return


#1

Hi,

I’m new to new relic, I just want to know what would be the difference between the codes below.

A.) return $browser.takeScreenshot();
B.) $browser.takeScreenshot();

A.) .then(function() {
return $browser.findElement(By.css(‘name=name’)).click();
})
.then(function(){
$browser.sleep(5000);
})
.then(function(){
$browser.takeScreenshot();
});
B.) .then(function() {
return $browser.findElement(By.css(’.submit.main.btn.ng-binding’)).click()
.then(function(){
$browser.sleep(5000)
.then(function(){
$browser.takeScreenshot();
})
})
})


#2

Hi @markj.v

basically to sequence the actions, we need to use the .then(function)

if you are using just $browser.takescreenshot, that it will be executed at any sequence, where as if you use it in .then function and do a return that it comes in the sequence it is.

( as per the documentation) Because WebDriverJS is asynchronous, scripting actions can sometimes execute out of order. For example, in a script containing three functions, the third function might execute before the second function. To force script actions to execute in order, wrap each action in a then(function(){}) call

https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/write-scripted-browsers

Hope this help.

thanks


#4

Hi @MKhanna, the link you provided is very helpful, but why does the new relic work even i don’t append “return” for every action I make. It makes me confuse whether to use “return” for every action or not.


#5

Depending on the response times of each function, it is possible not to use the return statement but if any start to take longer you may see unexpected results.

Chaining the promises ensures that they always fire in the order you specify.


#6

Thank you @stefan_garnham, this is very helpful


#7

Awesome explanation @stefan_garnham I could not find words to explain this one.

thanks :slight_smile: I learnt something too :slight_smile:


#8

Hey @markj.v - welcome to the New Relic community! Looks like the three of you got this ironed out, but let us know if you are looking for anything else. Thanks @MKhanna and @stefan_garnham - ya’ll are amazing!