Instalasi
npm install node-plug
Contoh penggunaan:
- plugin.js
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!')
},
}
- main.js
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!',
])
Kode di atas berjalan dengan baik tapi jika tanpa plugin promise maka akan terjadi error.
Error karena tanpa plugin promise
Kenapa terjadi error? Karena method runPlugin mengembalikan promise.
Solusi:
Tambahkan plugin sync dan callback ke plugin promise.
- plugin.js
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!')
},
}*/
- main.js
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!',
])*/
Top comments (0)