🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

tick-promise

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tick-promise

A promise wrapper for process nextTick

1.0.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

process.nextTick promise wrapper

There are cases that process nextTick needs to be used in ES 2017 async functions:

const nextTick = require('tick-promise');

(async () => {

  // Some code that uses process.nextTick

  await nextTick();

  // Do things after process.nextTick

})()

Install

npm install tick-promise

Demo / Use case

For an example we'll use 2 nice node modules, prompts for retrieving user input and simple-node-logger to create our logs:

const prompts = require('prompts');
const log = require('simple-node-logger').createSimpleLogger();

(async () => {
  log.info('Asking user age');

  const response = await prompts({
    type: 'number',
    name: 'value',
    message: 'How old are you?',
    validate: value => value < 18 ? `Nightclub is 18+ only` : true
  });
 
  console.log(response); // => { value: 24 }
})();

If we execute this, we'll get something like this:

Demo problem

We can see that the logger text output is next to the prompt output, not good. That happens because simple-node-logger uses process.nextTick to do the output. In order to avoid this, we need to create a promise wrapper for process.nextTick and use it after the logger and before the prompt:

const prompts = require('prompts');
const log = require('simple-node-logger').createSimpleLogger();
const nextTick = require('tick-promise');

(async () => {
  log.info('Asking user age');

  await nextTick();

  const response = await prompts({
    type: 'number',
    name: 'value',
    message: 'How old are you?',
    validate: value => value < 18 ? `Nightclub is 18+ only` : true
  });
 
  console.log(response); // => { value: 24 }
})();

Now we get the proper output result 👍

Demo fix

License

MIT License

Keywords

process

FAQs

Package last updated on 24 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts