登录
首页 >  文章 >  前端

简单的 Nodejs 插件支持同步、回调、承诺和断言

来源:dev.to

时间:2025-01-13 16:33:17 176浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《简单的 Nodejs 插件支持同步、回调、承诺和断言》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

安装

npm install node-plug

使用示例:

  • 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!')
  },
}
  • 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!',
])

上面的代码工作正常,但是如果没有 promise 插件,就会发生错误。

image description

由于没有 promise 插件而导致的错误
为什么会出现错误?因为 runplugin 方法返回一个承诺。

解决方案:
为 promise 插件添加同步和回调插件。

  • 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!')
  },
}*/
  • 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!',
])*/

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《简单的 Nodejs 插件支持同步、回调、承诺和断言》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>