How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • import async_hooks from 'async_hooks';
    
    // Return the ID of the current execution context.
    const eid = async_hooks.executionAsyncId();
    
    // Return the ID of the handle responsible for triggering the callback of the
    // current execution scope to call.
    const tid = async_hooks.triggerAsyncId();
    
    // Create a new AsyncHook instance. All of these callbacks are optional.
    const asyncHook =
        async_hooks.createHook({ init, before, after, destroy, promiseResolve });
    
    // Allow callbacks of this AsyncHook instance to call. This is not an implicit
    // action after running the constructor, and must be explicitly run to begin
    // executing callbacks.
    asyncHook.enable();
    
    // Disable listening for new asynchronous events.
    asyncHook.disable();
    
    //
    // The following are the callbacks that can be passed to createHook().
    //
    
    // init() is called during object construction. The resource may not have
    // completed construction when this callback runs. Therefore, all fields of the
    // resource referenced by "asyncId" may not have been populated.
    function init(asyncId, type, triggerAsyncId, resource) { }
    
    // before() is called just before the resource's callback is called. It can be
    // called 0-N times for handles (such as TCPWrap), and will be called exactly 1
    // time for requests (such as FSReqCallback).
    function before(asyncId) { }
    
    // after() is called just after the resource's callback has finished.
    function after(asyncId) { }
    
    // destroy() is called when the resource is destroyed.
    function destroy(asyncId) { }
    
    // promiseResolve() is called only for promise resources, when the
    // resolve() function passed to the Promise constructor is invoked
    // (either directly or through other means of resolving a promise).
    function promiseResolve(asyncId) { }
    Docs
  • const async_hooks = require('async_hooks');
    
    const asyncHook = async_hooks.createHook({
      init(asyncId, type, triggerAsyncId, resource) { },
      destroy(asyncId) { }
    });
    Docs
  • import { createHook } from 'async_hooks';
    
    const asyncHook = createHook({
      init(asyncId, type, triggerAsyncId, resource) { },
      destroy(asyncId) { }
    });
    Docs
  • const { createServer } = require('http');
    const {
      executionAsyncId,
      executionAsyncResource,
      createHook
    } = require('async_hooks');
    const sym = Symbol('state'); // Private symbol to avoid pollution
    
    createHook({
      init(asyncId, type, triggerAsyncId, resource) {
        const cr = executionAsyncResource();
        if (cr) {
          resource[sym] = cr[sym];
        }
      }
    }).enable();
    
    const server = createServer((req, res) => {
      executionAsyncResource()[sym] = { state: req.url };
      setTimeout(function() {
        res.end(JSON.stringify(executionAsyncResource()[sym]));
      }, 100);
    }).listen(3000);
    Docs
  • 'use strict';
    const async_hooks = require('async_hooks');
    const {
      performance,
      PerformanceObserver
    } = require('perf_hooks');
    
    const set = new Set();
    const hook = async_hooks.createHook({
      init(id, type) {
        if (type === 'Timeout') {
          performance.mark(`Timeout-${id}-Init`);
          set.add(id);
        }
      },
      destroy(id) {
        if (set.has(id)) {
          set.delete(id);
          performance.mark(`Timeout-${id}-Destroy`);
          performance.measure(`Timeout-${id}`,
                              `Timeout-${id}-Init`,
                              `Timeout-${id}-Destroy`);
        }
      }
    });
    hook.enable();
    
    const obs = new PerformanceObserver((list, observer) => {
      console.log(list.getEntries()[0]);
      performance.clearMarks();
      performance.clearMeasures();
      observer.disconnect();
    });
    obs.observe({ entryTypes: ['measure'], buffered: true });
    
    setTimeout(() => {}, 1000);
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples