安装
1
npm install node-plug
使用示例:
plugin.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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!)
},
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 插件添加同步和回调插件。
plugin.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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!)
},
}*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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!,
])*/
以上就是简单的 Nodejs 插件支持同步、回调、承诺和断言的详细内容,更多请关注php中文网其它相关文章!