在我的"app.js“中,我有一个异步函数等待来自我导入的connection.js的连接准备就绪
我不确定如何才能让app.js在“等待”的情况下正常工作。在connection.js中,我无法在' on‘函数中添加导出,也无法在on函数之外添加导出。
我还在学习promises/await's等,所以在正确方向上的指点将不胜感激。
app.js
var qlabconn = require('./qlab/connection.js');
// Wait for QLab connection, then we'll start the magic!
(async () => {
console.log('Ready!');
var qlabConnectionReady = await qlabconn.connectionReady;
//qlab.cues.x32_mute('1', "OFF");
console.log(qlabConnectionReady);
})();connection.js
// On connection to QLab
qlabconn.on('ready', () => {
console.log('Connected to QLab!');
let connectionReady = new Promise(resolve => {
resolve(true)
});
(async () => {
await core.send_message(`/alwaysReply`, {"type" : 'f', "value" : 1})
})();
});发布于 2019-09-17 00:38:19
如果您需要根据回调的结果获取promise,则应该将逻辑包装在新的Promise回调中。例如,在connection.js中:
// return a reference to qlabconn once we have established a connection
module.exports = function getConnection() {
return new Promise((resolve, reject) => {
// let qlabconn = new QLabConnection(...)
qlabconn.on('ready', () => resolve(qlabconn));
})
}然后,我们可以像在app.js中一样使用connection对象。
const getConnection = require('./connection');
(async () => {
let qlabconn = await getConnection();
console.log(qlabconn.connectionReady);
await core.send_message(`/alwaysReply`, {type: 'f', value: 1});
console.log('Connected to QLab!');
// qlab.cues.x32_mute('1', 'OFF');
})();https://stackoverflow.com/questions/57960903
复制相似问题