F
F
Forever Extreme2021-09-08 10:42:18
JavaScript
Forever Extreme, 2021-09-08 10:42:18

How can the backend refactor this code to js?

Hello. As a back-end developer, due to circumstances beyond my control, I had to do some work on the front. There is bitrix24api . There are methods there (almost all of them) from which it is not so easy (for me) to get values .
For example :

BX24.callMethod('user.current', {}, function(res){
    alert('Привет, ' + res.data().NAME + '!');
});

Up to this point, it was enough to call all the methods in callbacks (yes, I know that this is not entirely correct, but I hoped that this burden would be removed from me soon)
But after getting tired of such nesting, I did (thanks to Google) like this:
const currentUser = () => {
    return new Promise((resolve, reject) => {
        BX24.callMethod('user.current', {}, function (res) {
            resolve(res.data())
        })
    })
}

async function getCurrentUser() {
    let result = await currentUser()
    return result
}
// и в нужно месте кода затем вызываю
let user = getCurrentUser()

Help me refactor this, please, because you need to write at least 2 constructs for each api method....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-09-08
@vechnokrainii

It seems that everything has already been invented before you https://github.com/andrey-tech/bx24-wrapper-js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question