When using Promises I am getting below error
‘Promise’ only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the lib
compiler option to es2015 or later.
When using Promises I am getting below error
‘Promise’ only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the lib
compiler option to es2015 or later.
How do I resolve this error?
@Sarang.Jaiswal Welcome to the community I’m not familiar with this “Promise” error. Can you tell me which agent or product you are using?
Hi @Sarang.Jaiswal, I’m guessing you are seeing this error when trying to use native promises in Synthetics scripts - please let me know if I’m mistaken.
In order to use native promises in Synthetics, you have to wrap it in $browser.executeScript
. I’ve written an example which can be found here, as well as a couple of other options.
Please let us know if you have any questions.
@daffinito Yes that is correct. I was getting the error while using the synthetics scripts. I am writing the scripts for testing API’s
do you have example for API’s?
Problem Statement: I have 2 API endpoint which are dependent on each other. the 1st endpoint returns the access token which is used in the 2nd endpoint to get the data.
is there a way to chain the request or controlling the sequence of send API request? I am guessing be default $http is async.
Thanks!
In addition to @philweber’s solution of nesting the second request in the callback of the first, if you want to use promises, you could use Q
in an API test. Here’s an example:
var Q = require('q');
var postWithPromise = function (opts) {
var deferred = Q.defer();
$http.post(opts, function (err, response, body) { deferred.resolve({ err: err, statusCode: response.statusCode, body: body }) } );
return deferred.promise;
};
var firstRequestOpts = {
uri: 'http://httpbin.org/post',
json: {
widgetType: 'gear',
widgetCount: 10
}
}
var secondRequestOpts = {
uri: 'http://httpbin.org/post',
json: {
widgetType: 'gear',
widgetCount: 5
}
}
postWithPromise(firstRequestOpts).then(function (response) {
console.log("1")
console.log("err " + response.err)
console.log("body " + JSON.stringify(response.body.json))
console.log("body " + response.statusCode)
}).then(function() {
return postWithPromise(secondRequestOpts)
}).then (function (response) {
console.log("2")
console.log("err " + response.err)
console.log("body " + JSON.stringify(response.body.json))
console.log("body " + response.statusCode)
})