Backendless Support
 
Answered

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?

  1. getQuotes(t, maxR, id, size, d, s) {
  2. // return `Hello t-${t}, maxR-${maxR}, id-${id}, size-${size}, d-${d}, s-${s}`;
  3. var whereClause = "";
  4. var dataQuery = {
  5. options : {
  6. },
  7. };
  8. whereClause = "id = 11586";
  9. dataQuery.condition = whereClause;
  10. return Backendless.Persistence.of(Quote).find(dataQuery, new Backendless.Async(function(e){
  11. // response.send(e.data);
  12. return "Hello";
  13. }));
  14. }

Is this code correct?

More specifically, is this one?

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

Thank you so much

Leave a Comment

Comments (3)

photo
1

Hi Tapuat,

Actually, you should return promise from your service method

  1. 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

  1. Backendless.enablePromises();

Regards, Ilya

photo
1

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

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

photo
1

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

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

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

  1. 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

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

For writing the service you should use promises.

Regards, Ilya