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

Sequence steps with Q



This is a somewhat contrived example and draws upon data values existing from my previous Insights POST script

This script shows how to use the Q library to convert the callbacks returned by the $http object in Synthetics into promises that can be executed sequentially to step through results.

var myLicenseKey = '{INSIGHTS QUERY KEY}';
var myInsertKey = '{INSIGHTS INSERT KEY}';
var accountNum =  'ACCOUNT NUMBER';
var Q = require('q');
var assert = require('assert');

var InsightsGET = function () {
var deferred = Q.defer();
var NRQL = 'SELECT%20average(amount)%20FROM%20SyntheticsEvent';
var options = {
  uri: ''+accountNum+'/query?nrql='+NRQL,
  headers: {
  'X-Query-Key': myLicenseKey,
  'Accept': 'application/json'
$http.get(options,function(err, response, body){
  if (err) deferred.reject(err); //rejects the promise with `err` as the reason
    else deferred.resolve(body); //fulfills the promise with `response and body as the values
return deferred.promise; //promise is returned

var processResults = function(data) {
var  deferred = Q.defer();
  var info = JSON.parse(data); //parse the results to pick up the average value
  console.log("Average value is: "+ info.results[0].average);
  return deferred.promise;

var InsightsPOST = function(average) {
var deferred = Q.defer();
var options = {
        url: ""+accountNum+"/events",
        body: '[{"eventType":"SyntheticsAverage","average":'+average+'}]',
        headers: {
                'X-Insert-Key': myInsertKey,
                'Content-Type': 'application/json'
$, function(error, response, body) {
  if (error) deferred.reject(error); //rejects the promise with `err` as the reason
    else deferred.resolve(average); //fulfills the promise with `average as the value, carrying the previous result along to the final step
return deferred.promise;};

var AssertResults = function(data) {
  assert.ok(data > 1000, "Average should be greater than 1000, value was "+data);
console.log("all done now");


Extract and reuse token