安装
npm install node-plug
使用示例:
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!') }, }
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 插件,就会出现错误。
由于没有 Promise 插件而导致的错误
为什么会出现错误?因为 runPlugin 方法返回一个承诺。
解决方案:
为 Promise 插件添加同步和回调插件。
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!') }, }*/
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!', ])*/
以上是Simpel 插件 Node.js 同步、回调、承诺、断言。的详细内容。更多信息请关注PHP中文网其他相关文章!