安裝
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中文網其他相關文章!