Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Promises and callbacks #18

Open
positlabs opened this issue May 19, 2017 · 0 comments
Open

Promises and callbacks #18

positlabs opened this issue May 19, 2017 · 0 comments

Comments

@positlabs
Copy link

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){})
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant