Installation
npm install node-plug
Exemple d'utilisation :
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!', ])
Le code ci-dessus fonctionne correctement, mais sans le plugin de promesse, une erreur se produira.
Erreur due à un plugin sans promesse
Pourquoi une erreur se produit-elle ? Parce que la méthode runPlugin renvoie une promesse.
Solution :
Ajoutez des plugins de synchronisation et de rappel au plugin de promesses.
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!', ])*/
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!