0

I've been scratching my head over on how to promisify this call and return data to another function when it becomes available.

syno.query('/webapi/query.cgi', {
    api    : 'SYNO.API.Info',
    version: 1,
    method : 'query',
    query  : 'ALL'
}, function(err, data) {
    if (err) return console.error(err);
    console.log(data);
});

Can anybody shed some light here ? I'm new to nodejs

the old library that I'm using is this one https://www.npmjs.com/package/synology

ddmunhoz
  • 31
  • 4

1 Answers1

1

try :

const query = () => new Promise((resolve, reject) => {

    syno.query('/webapi/query.cgi', {
        api: 'SYNO.API.Info',
        version: 1,
        method: 'query',
        query: 'ALL'
    }, function(err, data) {
        if (err) reject(err);
        resolve(data);
    });

})

then you can call :

query().then(data => { //do something
    })
    .catch(e => { //do other thing
    })
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48