unofficial_nodejs_nest icon indicating copy to clipboard operation
unofficial_nodejs_nest copied to clipboard

Promises and callbacks

Open positlabs opened this issue 7 years ago • 0 comments

It would be nice to know when the nest has completed an action, or if it failed. We could add Promise and callback support to address this.

var setTemperature = function (deviceId, tempC, optionalCallback) {

    var _resolve, _reject
    var promise = new Promise((resolve, reject) => {
        _resolve = resolve
        _reject = reject
    })
    // ...
    nestPost({
        path:'/v2/put/shared.' + deviceId,
        body:body,
        headers:headers,
        done:function (data) {
            if(optionalCallback) optionalCallback(null, data)
            _resolve(data)
        },
        error: function(err){
            if(optionalCallback) optionalCallback(err)
            _reject(err)
        }
    });

    return promise
}

// with a promise
setTemperature(22)
    .then(data => { console.log('temperature was set!!') })
    .catch(err => { console.log('failed to set temperature :[') })

// with a callback
setTemperature(22, function(err, data){})

positlabs avatar May 19 '17 13:05 positlabs