question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an async init hook

See original GitHub issue

It would we good to have an asynchronous called init hook.

I tried to implement a helper for the selenium-standalone package. Ideally it should use the _init hook of a helper. So my code looked like this:

const selenium = require('selenium-standalone');

'use strict';

class SeleniumHelper extends Helper {

  _init() {
    return selenium.install({logger: function(message) {
      console.log(message);
    }}, function() {
      selenium.start(() => {console.log('blub')});
    });
  }
}

module.exports = SeleniumHelper;

The problem is here, that the _init hook is called synchronously in the lib/listener/helpers.js

event.dispatcher.on(event.all.before, function () {
  runHelpersHook('_init');
});

This means, that the init will not wait for the selenium helper to be correctly initialized, so further steps will fails as selenium wasn’t started. Also the beforeSuite step is not helping as it is also called synchronously. Only the before hook would work.

By changing the above dispatch event to

event.dispatcher.on(event.all.before, function () {
  runAsyncHelpersHook('_init');
});

The init would wait for the initialization of the selenium standalone to complete. But as there may be code in the wild relying on synchronous initialization, you may introduce a new hook which is called asynchronously.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
patmeekercommented, Aug 8, 2017

I also need this!

0reactions
DavertMikcommented, Sep 5, 2021

We have a new event/plugin system for that

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async hooks | Node.js v19.3.0 Documentation
init (asyncId, type, triggerAsyncId, resource) # ... Called when a class is constructed that has the possibility to emit an asynchronous event. This...
Read more >
How to set class attribute with await in __init__ - Stack Overflow
class aobject(object): """Inheriting this class allows you to define an async __init__. So you can create objects by doing something like `await ......
Read more >
Exploring Node.js Async Hooks - AppSignal Blog
Let's experiment with Node.js async hooks. ... init : as the name suggests, it's called when that specific async resource is initialized.
Read more >
Understanding Async Resources with Async Hooks
In this article, I'm going to explain the life cycle of an asynchronous resource in NodeJS with the help of async hooks.
Read more >
async hooks
triggerAsyncId(); // Create a new AsyncHook instance. All of these callbacks are optional. const asyncHook = async_hooks.createHook({ init ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found