설치
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 플러그인이 없으면 오류가 발생합니다.
약속 없는 플러그인으로 인한 오류
왜 오류가 발생합니까? runPlugin 메소드가 promise를 반환하기 때문입니다.
해결책:
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!