Check that a cancel function exists before calling it on a promise

Fixes #151.