D
D
Danil Emelyanov2017-01-16 21:44:28
JavaScript
Danil Emelyanov, 2017-01-16 21:44:28

How to competently rewrite collback to promise?

There are functions with similar content. A request is sent through the Bitrix24 rest api, we receive data, process, write ....
Now I put a flag in each such function, if they are all true, then the finishInstall function is launched, which collects all the data received before. functions (similar to y in the example).
How to convert this scheme to promise? There are already a lot of callbacks and flags...

application.prototype.getUsers = function () {
    BX24.callMethod('user.get', {sort:'ID',order:'ASC'}, function(result) {
        
        if(result.error()) {
           // ошибки
        }
        else {
            // записываю результаты в массив
            if(result.more()) {  // result ограничен в 50 записей
                result.next(); // показать след. 50
            }
            else {
                // конец
               // тут сейчас флаги this.appLoadedKeys['user-list'].loaded = true;
              this.isDataLoaded();
            }
        }
    });
};

Just learning JS :)
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dark Hole, 2017-01-16
@abyrkov

Dock:
https://learn.javascript.ru/promise
https://developer.mozilla.org/ru/docs/Web/JavaScri...
Example:

promiseFunction = (resolve, reject) => setTimeout(resolve, 100);
var promise = new Promise(promiseFunction);
promise
  .then(promiseFunction)
  .then(promiseFunction)
  ...

How exactly in your case - do not write without clarification.
PS It's better to learn first, then ask questions xD

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question