Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Some hints about the usage of promises #4

@sweetpi

Description

@sweetpi

Great work. I like the plugin very much. However, unfortunately I don't have a ios device for testing.

I did spot some errors in the code, I want to point out to improve it a little bit:

device.changeStateTo(value).then( callback() )

You are calling the callback, before the actions finished, because you are passing the result of the callback call to the then handler. You probably call the callback in the then handler:

device.changeStateTo(value).then( => callback() )

This parses a function, that calls the callback to the handler. Which is the right way.

Further you should handle errors:

device.changeStateTo(value)
  .then( => callback() )
  .catch( (error) => callback(error) )
  .done()

catch catches an error and passes it to the callback. done throws all unhandled errors.


...
        device.turnOff().then(
          device.turnOn().then(
            device.turnOff().then(
            ...

The same problem here. You parsing the result of device.turnOn() to the then handler. However you want to pass a function, that calls turnOn:

...
        device.turnOff().then( =>
          device.turnOn().then( =>
            device.turnOff().then( =>
            ...

You can even flatten the callback chain here:

      device.getState().then( (state) =>
       device.turnOff()
      ).then( =>
        device.turnOn()
      ).then( => 
        device.turnOff()
      ).then( =>
        device.turnOn()
      ).then( =>
        # recover initial state
        device.turnOff() if not state
      )
      .then( => callback() )
      .catch( (error) => callback(error) )
      .done()

As rule of thumb: Always make sure to pass a function => ... to a then handler. Further end each chain of promise class with a .catch( (error) => callback(error) ), if you want to pass the error to a callback and with .done() to not silently discard errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions