admin管理员组文章数量:1304049
Consider the following case:
const waitForEvent = async (api) => {
api.on('eventOne', () => {
return 'eventOne';
})
api.on('eventTwo', () => {
return 'eventTwo';
})
api.on('eventThree', () => {
return 'eventThree';
})
api.load();
}
What I am trying to do is setup event callbacks on the api
variable inside the async function, trigger the api.load()
function, and then return the event that happened first, in this case either eventOne|eventTwo|eventThree
Problem is, this syntax is bad, and this example does not work. I couldn't find any way to achieve this using async/await and had to revert to promises like this:
const waitForEvent = (api) => {
return new Promise(resolve) => {
api.on('eventOne', () => {
resolve('eventOne');
})
api.on('eventTwo', () => {
resolve('eventTwo');
})
api.on('eventThree', () => {
resolve('eventThree');
})
api.load();
}
}
So my question is, can this be acplished using async/await? Anyway this can be done using the new async/await es7 syntax?
Consider the following case:
const waitForEvent = async (api) => {
api.on('eventOne', () => {
return 'eventOne';
})
api.on('eventTwo', () => {
return 'eventTwo';
})
api.on('eventThree', () => {
return 'eventThree';
})
api.load();
}
What I am trying to do is setup event callbacks on the api
variable inside the async function, trigger the api.load()
function, and then return the event that happened first, in this case either eventOne|eventTwo|eventThree
Problem is, this syntax is bad, and this example does not work. I couldn't find any way to achieve this using async/await and had to revert to promises like this:
const waitForEvent = (api) => {
return new Promise(resolve) => {
api.on('eventOne', () => {
resolve('eventOne');
})
api.on('eventTwo', () => {
resolve('eventTwo');
})
api.on('eventThree', () => {
resolve('eventThree');
})
api.load();
}
}
So my question is, can this be acplished using async/await? Anyway this can be done using the new async/await es7 syntax?
Share Improve this question edited May 3, 2017 at 6:16 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked May 2, 2017 at 20:59 aaaaaa 6016 silver badges13 bronze badges 4-
One problem is that your
return
statements are returning their value from their specific event handler functions, not the mainwaitForEvent
function – Will P. Commented May 2, 2017 at 21:03 - Of course, I know that :) thanks for pointing that out. I am trying to figure a way to go around that. – aaa Commented May 2, 2017 at 21:07
-
I don't get what you mean by "I had to revert to promises".
async
/await
uses promises anyway? And ifapi
is not promisified, you need to use thePromise
constructor. – Bergi Commented May 2, 2017 at 21:55 - What's the meaning of eventOne, eventTwo, eventThree? Is it "one of them" or "any number of them in any order" or "all 3 in a random order"? I'm asking because maybe promises are not the best abstraction for your problem. – Kos Commented Aug 23, 2017 at 9:19
1 Answer
Reset to default 10Since async/await
allows us to write async constructs in a synchronous-looking manner (lexical top-down), there isn't really a specific approach to execute 3 different lines of code (or more accurately, statements) simultaneously.
The ideal api for this is the Promise.race
.
First you convert your api callback into returning a promise:
const apiPromiseBuilder = (api) => (eventName) => new Promise(resolve => api.on(eventName, () => {
resolve(eventName);
}));
Then you race all the events you need:
const waitForEvent = (api) => {
const apiPromise = apiPromiseBuilder(api);
const promiseRace = Promise.race([
apiPromise('eventOne'),
apiPromise('eventTwo'),
apiPromise('eventThree')
]);
api.load();
return promiseRace;
};
Or using async/await
:
async function waitForEvent(api) {
const apiPromise = apiPromiseBuilder(api);
const promiseRace = Promise.race([
apiPromise('eventOne'),
apiPromise('eventTwo'),
apiPromise('eventThree')
]);
api.load();
const firstResult = await promiseRace;
return firstResult;
};
本文标签: asynchronousCan I use asyncawait to wait for multiple events in JavaScriptStack Overflow
版权声明:本文标题:asynchronous - Can I use asyncawait to wait for multiple events in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741778059a2397144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论