Installation
npm install node-plug
Anwendungsbeispiel:
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!', ])
Der obige Code läuft einwandfrei, aber ohne das Promise-Plugin tritt ein Fehler auf.
Fehler aufgrund eines fehlenden Promise-Plugins
Warum tritt ein Fehler auf? Weil die runPlugin-Methode ein Versprechen zurückgibt.
Lösung:
Fügen Sie Synchronisierungs- und Rückruf-Plugins zum Versprechens-Plugin hinzu.
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!', ])*/
Das obige ist der detaillierte Inhalt vonEinfaches Node.js-Plugin zur Synchronisierung, Rückruf, Versprechung und Bestätigung.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!