I've been writing a Cordova plugin, following this documentation. I'm able to install the plugin without issues, however, when calling some asynchronous code in my hook, that code will never run (or at least thats my suspicion). I've tested the following code:
In my plugin.xml
i've defined the following hook:
<hook type="after_run" src="lib/after-run.js" />
My after-run.js
looks like this:
module.exports = function () {
console.log('After run!')
function timeout() {
console.log('Timeout!')
setTimeout(function () {
console.log('Hello World!')
timeout()
}, 1000)
}
timeout()
}
When running cordova run
, i get the following output:
$ cordova run
... (platform building etc) ...
After run!
Timeout!
io.cordova.hellocordova: 68915
logPath: /Users/niklas/temp/temp/sampleApp/platforms/ios/cordova/console.log
I was, however, expecting to get something like this:
$ cordova run
... (platform building etc) ...
After Run!
Timeout!
Hello World!
Timeout!
Hello World!
Timeout!
Hello World!
...
So my question is: why is this? And how do i get the wanted behaviour? i.e. how do I specify when my hook is finished, instead of it just being "cut off".
Notes:
- I'm testing this with a clean Cordova project using the iOS platform and no other plugins.
- There's no log available at the location specified in the console output.
- When testing
after-run.js
using node, I get the expected output. - I've also tried the 'promise' approach specified here, with similar results to my own test above.
after_run
hook - it's not used very often, so I bet it doesn't get much testing. – DaveAlden Nov 16 '17 at 10:02