Answer the question
In order to leave comments, you need to log in
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();
}
}
});
};
Answer the question
In order to leave comments, you need to log in
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)
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question