Home > Web Front-end > JS Tutorial > Simpel plugin Node.js mendukung sync, callback, promise, dan assert.

Simpel plugin Node.js mendukung sync, callback, promise, dan assert.

Susan Sarandon
Release: 2025-01-03 04:43:38
Original
631 people have browsed it

Installation

npm install node-plug
Copy after login

Example of use:

  • plugin.js
export const pluginSync = {
  run() {
    console.log('Plugin sync dijalankan!')
  },
}

export const pluginCallback = {
  run() {
    console.log('Plugin callback mulai...')
    setTimeout(() => console.log('Plugin callback selesai!'), 15)
  },
}

export const pluginPromise = {
  async run() {
    console.log('Plugin Promise mulai...')
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin Promise selesai!')
  },
}
Copy after login
  • main.js
import { addPlugin, runPlugin, test } from 'node-plug'
import { pluginSync, pluginCallback, pluginPromise } from './plugin.js'

// Menambahkan plugin
addPlugin(pluginSync)
addPlugin(pluginCallback)
addPlugin(pluginPromise)

// Menjalankan plugin
runPlugin()

// Melakukan pengujian
test([
  'Plugin sync dijalankan!',
  'Plugin callback mulai...',
  'Plugin Promise mulai...',
  'Plugin callback selesai!',
  'Plugin Promise selesai!',
])
Copy after login

The code above runs fine, but without the promise plugin an error will occur.

Simpel plugin Node.js mendukung sync, callback, promise, dan assert.

Error due to no promise plugin
Why does an error occur? Because the runPlugin method returns a promise.

Solution:
Add sync and callback plugins to promises plugin.

  • plugin.js
const pluginSync = {
  run() {
    console.log('Plugin sync dijalankan!')
  },
}

const pluginCallback = {
  run() {
    console.log('Plugin callback mulai...')
  },
}

export const pluginPromise = {
  async run() {
    pluginSync.run()
    pluginCallback.run()
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin callback selesai!')
  },
}

// atau
/*const pluginSync = {
  run() {
    console.log('Plugin sync mulai...')
  },
}

const pluginCallback = {
  run() {
    console.log('Plugin callback mulai...')
  },
}

export const pluginPromise = {
  async run() {
    pluginSync.run()
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin sync selesai!')
    pluginCallback.run()
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin callback selesai!')
  },
}*/

// atau
/*const pluginSync = {
  run() {
    console.log('Plugin sync mulai...')
  },
}

const pluginCallback = {
  run() {
    console.log('Plugin callback mulai...')
  },
}

export const pluginPromise = {
  async run() {
    pluginSync.run()
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin sync selesai!')
    pluginCallback.run()
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin callback selesai!')
    console.log('Plugin Promise mulai...')
    await new Promise((resolve) => setTimeout(resolve, 15))
    console.log('Plugin Promise selesai!')
  },
}*/
Copy after login
  • main.js
import { addPlugin, runPlugin, test } from 'node-plug'
import { pluginPromise } from './plugin.js'

// Menambahkan plugin
addPlugin(pluginPromise)

// Menjalankan plugin
runPlugin()

// Melakukan pengujian
test([
  'Plugin sync dijalankan!',
  'Plugin callback mulai...',
  'Plugin callback selesai!',
])

// atau
/*import { addPlugin, runPlugin, test } from 'node-plug'
import { pluginPromise } from './plugin.js'

// Menambahkan plugin
addPlugin(pluginPromise)

// Menjalankan plugin
runPlugin()

// Melakukan pengujian
test([
  'Plugin sync mulai...',
  'Plugin sync selesai!',
  'Plugin callback mulai...',
  'Plugin callback selesai!',
])*/

// atau
/*import { addPlugin, runPlugin, test } from 'node-plug'
import { pluginPromise } from './plugin.js'

// Menambahkan plugin
addPlugin(pluginPromise)

// Menjalankan plugin
runPlugin()

// Melakukan pengujian
test([
  'Plugin sync mulai...',
  'Plugin sync selesai!',
  'Plugin callback mulai...',
  'Plugin callback selesai!',
  'Plugin Promise mulai...',
  'Plugin Promise selesai!',
])*/
Copy after login

The above is the detailed content of Simpel plugin Node.js mendukung sync, callback, promise, dan assert.. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template