Skip to content

Commit

Permalink
fix: support async callbacks (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Apr 4, 2023
1 parent 43be343 commit 5476a7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = defineConfig({
// baseUrl, etc
supportFile: false,
fixturesFolder: false,
video: false,
setupNodeEvents(cypressOn, config) {
const on = require('.')(cypressOn)
on('before:browser:launch', (browser, launchOptions) => {
Expand All @@ -15,7 +16,12 @@ module.exports = defineConfig({
})

on('after:spec', (a) => {
console.log('after spec 1', a.relative)
return new Promise((resolve) => {
setTimeout(() => {
console.log('after spec 1', a.relative)
resolve()
}, 100)
})
})
on('after:spec', (a) => {
console.log('after spec 2', a.relative)
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ function onProxy(on) {
} else {
debug('the only listener so far')
listeners[eventName] = [callback]
on(eventName, function () {
on(eventName, async function () {
debug(
'proxy %s to %d listeners',
eventName,
listeners[eventName].length,
)
let result
listeners[eventName].forEach((fn) => {
result = fn.apply(null, arguments)
})
// support async callbacks
for (let fn of listeners[eventName]) {
result = await fn.apply(null, arguments)
}
// return the last result
return result
})
Expand Down

0 comments on commit 5476a7e

Please sign in to comment.