Timeout check

Hi
Following these instructions we are using async code. However, we are not 100% sure of the code. Can you let us know if it is correct?


getQuotes(t, maxR, id, size, d, s) {
// return `Hello t-${t}, maxR-${maxR}, id-${id}, size-${size}, d-${d}, s-${s}`;
var whereClause = "";
var dataQuery = {
options : {
},
};
whereClause = "id = 11586";
dataQuery.condition = whereClause;
return Backendless.Persistence.of(Quote).find(dataQuery, new Backendless.Async(function(e){
// response.send(e.data);
return "Hello";
}));
}

Is this code correct?
More specifically, is this one?


return Backendless.Persistence.of(Quote).find(dataQuery, new Backendless.Async(function{
// response.send(e.data);
return "Hello";
}));

Thank you so much

Hi Tapuat,
Actually, you should return promise from your service method

return Backendless.Persistence.of(Quote).find(dataQuery).then(function(){ /*something that should be run on succeed*/ }, function() {/*error handling*/});

For enabling promises, you should write the next code at the beginning of service description

Backendless.enablePromises();

Regards, Ilya

Is this code correct? If not, how to implement Promise?


return Backendless.Persistence.of(Quote).find(dataQuery, new Backendless.Async(function(e){
  // response.send(e.data);
  //return "Hello";
  return Promise.resolve("Hello");
 }));

Hi,
You don’t have to implement promises.
All that you need is call method Backendless.enablePromises().
It switches all API methods to the async mode and they all will return a promise as result of calling.
For example look at the method ‘find’.
It has two signature of calling
The synchronous call which returns a result of retrieving

var results = Backendless.Persistence.of(Quote).find(dataQuery);

The asynchronous call which throws a result of retrieving to the callback

Backendless.Persistence.of(Quote).find(dataQuery, new Backendless.Async(function(results) {}, function(error){}));

But if we call Backendless.enablePromises() in the beginning of the program the method ‘find’ will have only one signature of calling. It returns a promise which will be fulfilled by the results of retrieving

Backendless.enablePromises(); //it should be called once in the beginning of the program
//....
return Backendless.Persistence.of(Quote).find(dataQuery).then(function(results) {}, function(error){});

For writing the service you should use promises.

Regards, Ilya